logo       

Re: [Boston.pm] "exit(<expr>)" versus "exit <expr>": msg#00027

Subject: Re: [Boston.pm] "exit(<expr>)" versus "exit <expr>"
>>>>> "KS" == Kripa Sundar 
>>>>> <Kripa.Sundar-HKixBCOQz3hWk0Htik3J/w@xxxxxxxxxxxxxxxx> 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  ------  uri-X/06uaNR9nFmbZtjAW+qKA@xxxxxxxxxxxxxxxx  -------- 
http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
science.linguis...    culture.sf.lite...    video.mplayer.c...    yellowdog.gener...    ietf.rfc822/199...    emacs.help/2002...    redhat.release....    kernel.speakup/...    java.openejb.de...    debian.devel.gt...    xfree86.newbie/...    bug-tracking.ma...    pam/2003-05/msg...    games.devel.ope...    user-groups.lin...    music.pancham/2...    network.mq.deve...    web.html.genera...    arklinux.bugs/2...    linux.ecasound/...    qnx.openqnx.dev...    org.user-groups...    file-systems.sf...    trustix.contrib...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe