Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

"exit 1" and "exit(1)" are apparently not the same

by syphilis (Archbishop)
on Nov 19, 2023 at 05:47 UTC ( [id://11155691]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I guess it's accepted and expected, but I can't find any documentation that explains this differing behaviour.
Without the enclosing parens, the exit function behaves as I expect - otherwise the specified value is apparently ignored, and the exit call defaults to exit 0.
D:\>perl -wle "`exit 1`; print $?;" 256 D:\>perl -wle "`exit(1)`; print $?;" 0 D:\>perl -wle "`exit 2`; print $?;" 512 D:\>perl -wle "`exit(2)`; print $?;" 0 D:\>perl -wle "$x = 1;`exit $x`; print $?;" 256 D:\>perl -wle "$x = 1;`exit($x)`; print $?;" 0
What am I failing to understand ?

I'm seeing this behaviour on Windows 11 with perl-5.38.0 and perl-5.39.4. That's the only testing I've done.

Cheers,
Rob

Replies are listed 'Best First'.
Re: "exit 1" and "exit(1)" are apparently not the same
by roho (Bishop) on Nov 19, 2023 at 06:23 UTC
      ... you know that the "exit" return codes are from the exit command given to the Windows "cmd" display.

      Duh ... a more accurate appraisal would be .. you should have realized that the "exit" return codes are from the exit command given to the Windows "cmd" display :-(
      I needed to instead be looking at (eg):
      D:\>perl -le "$x = 1; system perl, '-le', \"exit ($x)\"; print $?;" 256 D:\>perl -le "$x = 1; system perl, '-le', \"exit $x\"; print $?;" 256
      Thanks roho !!

      Cheers,
      Rob
Re: "exit 1" and "exit(1)" are apparently not the same
by ikegami (Patriarch) on Nov 20, 2023 at 16:57 UTC

    I can't find any documentation that explains this differing behaviour.

    This is the documentation for exit (as produced by help exit):

    Quits the CMD.EXE program (command interpreter) or the current batch script. EXIT [/B] [exitCode] /B specifies to exit the current batch script instead of CMD.EXE. If executed from outside a batch script, it will quit CMD.EXE exitCode specifies a numeric number. if /B is specified, sets ERRORLEVEL that number. If quitting CMD.EXE, sets the p +rocess exit code with that number.

    The bevahiour of exit(1) isn't defined, and there's no reason to believe it's valid. (It's no like you can do echo(foo) or perl(file.pl), and exit(1) is no different.) You provided garbage, and received garbage in return (GIGO).

Re: "exit 1" and "exit(1)" are apparently not the same
by sectokia (Pilgrim) on Nov 20, 2023 at 03:07 UTC
    It tries to find ascii digits, and once it encounters something not an ascii digit it stops, with the default zero. So '(1)' is 0 and so is '(1' or '(' or 'a' but 1(0) is 1.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11155691]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-14 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found