print (1 ? 1 : 0) . 'x'

Luis Motta Campos monsieur_champs at yahoo.com.br
Thu May 11 12:37:34 BST 2006


O'Shaughnessy, Jamie wrote:
> print (1 ? 1 : 0) . 'x';
> 
> What do you expect this would print?
> 
> I expected the output would be "1x" - the ?: evaluates to 1, the ()s are needed because . Is higher precedence than ?: so you end up with 1 . 'x' as the parameter to print - hence print "1x".
> 
> However it doesn't, it just prints "1".
> 
> Can anyone explain what is happening here?
> 
> Changing the code to:
> 
> print (1 ? 1 : 0), 'x';
> 
> Also does the same, yet:
> 
> Print 1 ? 1 : 0, 'x';
> 
> Does print out "1x".
> 
> Also, taking the original code and assigning it to a variable:
> 
> my $x = (1 ? 1 : 0) . 'x';
> print $x;
> 
> Also prints out "1x"

   DWIM, almost:

   # Emacs, this seems to be -*- perl -*-
   print +( 1 ? 1 : 0 ) . 'x';
   # do what you meant in beggining.
   __END__

   This is mostly because of the Looks Like a Function, Then Its a 
Function principle. print() Looks A LOT like a function, and you doesn't 
seems to "use warnings;"... remember: warnings are your friends.

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  Luis Motta Campos is Software Engineer, Oracle OCP/DBA, Un*x
  Sysadmin, Member of {Lisbon,São Paulo,Cascavel,Brasil,London}
  Perl Mongers and Perl Fanatic Evangelist
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

		
_______________________________________________________ 
Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celular. Registre seu aparelho agora! 
http://br.mobile.yahoo.com/mailalertas/ 
 



More information about the london.pm mailing list