>>>>> "KS" == Kripa Sundar <[EMAIL PROTECTED]> writes:

  KS> Hello all,
  KS> "exit <expr>" apparently behaves differently from
  KS> "exit(<expr>)" when <expr> is not a simple scalar.
  KS> See the example below.

  KS> .....................\/.........BEGIN.........\/.....................
  KS> $ perl -e 'exit " @ARGV " =~ / h/ ? 0 : 1' hah && echo good

the / h/ is true and exits binds with that before ?: so the exit is 1
and no echo (see below)

  KS> $ perl -e 'exit(" @ARGV " =~ / h/ ? 0 : 1)' hah && echo good
  KS> good

the parens force exit with 0 and so it echoes.

  KS> $ perl -e 'my $x = " @ARGV " =~ / h/ ? 0 : 1; exit $x' hah && echo good
  KS> good

same as above. assignment is lower than ?: so $x is 0

  KS> $ perl -le 'print " @ARGV " =~ / h/ ? 0 : 1' hah && echo good
  KS> 0
  KS> good

that is a better example than you might realize. it prints 0 as you
expect but print itself returns 1 when it succeeds. that last evaluated
expression is the default for exiting a program by falling through. so
exit gets 1 and that causes no echo.

as to why the echo is backwards, look at these. the shell && are meant
to work with classic process exit codes where 0 is success and other
values are errors. perl has the problem of not inverting logic for the
result of system() calls and newbies fall for that trap all the time.

perl -e 'exit 1' && echo 'ok'
perl -e 'exit 0' && echo 'ok'
ok

so you might have your expected echo's backwards.

then from perldoc perlop (the op precedence table):

        <snip>
           left        =~ !~
        <snip>
           right       ?:
        <snip>
           nonassoc    named unary operators
        <snip>

  KS> "perldoc perlop" shows "=~" at level 6, and "? :" at level 18.

  KS> "perldoc -f exit" doesn't turn up anything weird either.

and exit would be named unary op and is a lower binding than =~ so that
will be evaluated before the exit. but ?: is lower than that and it will
be ignored.

another way to understand perl's precedence is to uncompile the program
using the deparse module (i always forget its name so someone else will
have to fill that in :). it will print out the same perl code with
parens as needed to show you what perl actually thinks is coded.

this is why i tend to like parens on some builtins, especially based on
whether they are standalone or in expressions.

uri

-- 
Uri Guttman  ------  [EMAIL PROTECTED]  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to