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 (Friar) 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.
Re: "exit 1" and "exit(1)" are apparently not the same
by ikegami (Patriarch) on Jan 15, 2025 at 13:54 UTC

    This has nothing to do with Perl. You are using cmd's exit. And there's no reason to believe the shell command exit(1) is valid.

    Correct usage:

    >cmd /c "exit 1" >echo %ERRORLEVEL% 1

    Incorrect usage:

    >cmd /c "exit(1)" >echo %ERRORLEVEL% 0

    I would have expected "is not recognized" or a syntax error. But whatever. Garbage in, garbage out.

    What happens is that exit(1) is treated as equivalent to exit (1), and (1) is numified to zero.

    >perl() Can't open perl script "()": No such file or directory
Re: "exit 1" and "exit(1)" are apparently not the same
by etj (Priest) on Jan 15, 2025 at 10:43 UTC
    The title of the OP needs changing to reflect that is within backquotes, on Windows. It is profoundly wrong as written now ("'exit 1' and 'exit(1)' are apparently not the same").
Re: "exit 1" and "exit(1)" are apparently not the same
by Anonymous Monk on Jan 15, 2025 at 12:45 UTC

    I'm curious what D:\>exit 0 displays for you. lol