timpoiko has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks. I've a question that bothers me.
timpoiko@ats:~$ cat test.pl
#!/usr/bin/perl use strict; use warnings; my $tmp = 2; my $var = 3; #Is $var greater than $tmp? print $var > $tmp ? "yes" : "no"; print "\n"; exit $var > $tmp ? 1 : 0;
When I ran this code, it printed exactly what I expected:
timpoiko@ats:~$ perl test.pl
yes
But however, the return value was completely unexpected:
timpoiko@ats:~$ echo $?
3
What went wrong? perldoc -f exit says that exit evaluates EXPR and exits immediately with that value. My operating system is Debian testing (Jessie).
timpoiko@ats:~$ perl -v

This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi
(with 41 registered patches, see perl -V for more detail)

Replies are listed 'Best First'.
Re: exit EXPR?
by AppleFritter (Vicar) on Jul 15, 2014 at 10:17 UTC

    What went wrong? perldoc -f exit says that exit evaluates EXPR and exits immediately with that value. My operating system is Debian testing (Jessie).

    Indeed, and it does just that. But you're basically doing this:

    ((exit $var) > $tmp) ? 1 : 0;

    In other words, exit is called with $var, and the result is then compared to $tmp, and the whole expression evalutes to 1 or 0... or would, if exit returned at all, which it of course doesn't.

    Here's a short illustration of how operator precedence works:

    #!/usr/bin/perl use feature qw/say/; use strict; use warnings; sub half($) { return $_[0] / 2; } say half 3 > 2 ? 7 : 5 ; # 5 say half(3 > 2) ? 7 : 5 ; # 7 say half(3 > 2 ? 7 : 5); # 3.5

    EDIT: to clarify this a bit more: named unary operators (of which exit is an example) bind more tightly than >, which binds more tightly than ?:, which in turn binds more tightly than list operators do (rightward). So ?: takes precedence over print and say -- but exit takes precedence over ?:.

    For more, see the "Operator Precedence and Associativity" table at the top of perlop, as well as chapter 3 ("Unary and Binary Operators") of Programming Perl.

      Just to be clear, my vote is for your EDIT. Before that, I could not see why there should be a difference between the print and exit statements.
      Bill
Re: exit EXPR?
by Anonymous Monk on Jul 15, 2014 at 10:39 UTC

    As Tutorials: Basic debugging checklist teaches, use B::Deparse to see how perl interprets the code

    $ perl -MO=Deparse,-p test.pl use warnings; use strict; (my $tmp = 2); (my $var = 3); print((($var > $tmp) ? 'yes' : 'no')); print("\n"); ((exit($var) > $tmp) ? '???' : '???'); test.pl syntax OK
Re: exit EXPR?
by Anonymous Monk on Jul 15, 2014 at 09:55 UTC

    Operator precedent is the reason. Try ...

    perl -e '$x = 4 ; exit $x > 3 ? 1 : 0' ; echo $? perl -e '$x = 5 ; exit ($x > 3) ? 1 : 0' ; echo $?

      ... or, perhaps parsing ...

      perl -e '$x = 6 ; exit( $x > 3 ? 1 : 0 )' ; echo $?
Re: exit EXPR?
by Anonymous Monk on Jul 15, 2014 at 11:31 UTC
    ( parentheses ( are ( not ( silly ) ) ) )