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

Jonathan Stowe jns at gellyfish.com
Thu May 11 11:59:05 BST 2006


On Thu, 2006-05-11 at 10:59, 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?
> 

As well as the switching on of warnings that everyone else is
advocating, light can sometimes be shone on confusing behaviour by using
B::Deparse:

        [jonathan at orpheus jonathan]$ perl -MO=Deparse
        print (1 ? 1 : 0) . 'x';
        __END__
        print(1) . 'x';
        __DATA__

/J\
> 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"
> 
> ???
> 
> Cheers,
> Jamie
-- 

This e-mail is sponsored by http://www.integration-house.com/



More information about the london.pm mailing list