in reply to Re: 255: perl compilation error code and ssh cannot connect code
in thread 255: perl compilation error code and ssh cannot connect code

Hi,

Testing this shows that it appears to work, even for compile-time* errors:

$ cat ex.pl #!/usr/bin/env perl use warnings; use strict; END { $? = 250 if $? == 255 } print $c; $ perl ex.pl ; echo $? Global symbol "$c" requires explicit package name (did you forget to d +eclare "my $c"?) at ex.pl line 7. Execution of ex.pl aborted due to compilation errors. 250

Note this is documented in $?. However, this solution means you'd have to insert the END block at the top of every script that you run like this, so personally I like Corion's suggestion of a wrapper script better. Also, consider what kind of errors you are catching. Syntax errors are something that you can catch when you're developing the script, so I don't see how those could sneak in. Runtime errors, like you mention in the bug report accessing something that doesn't exist, are something you could catch and handle gracefully within the script, for example there's Try::Tiny.

* Update: Actually I think this may be inaccurate, although it seems to catch many syntax errors and such, there might be cases where the END block isn't executed; if I get around to it I'll test more later.

Hope this helps,
-- Hauke D

  • Comment on Re^2: 255: perl compilation error code and ssh cannot connect code (updated)
  • Download Code

Replies are listed 'Best First'.
Re^3: 255: perl compilation error code and ssh cannot connect code (updated)
by i5513 (Pilgrim) on Nov 02, 2016 at 11:30 UTC
    Maybe good idea only change the result if it is executed via ssh:
    #!/usr/bin/env perl use warnings; use strict; END { $?=254 if defined $ENV{SSH_CONNECTION} and $? eq 255 } print $c;

      Hi i5513,

      Maybe good idea only change the result if it is executed via ssh

      Sure, I'd say that's a good idea. Just a minor nitpick, personally I'd test an environment variable for truth instead of definedness, since that allows me to do ENVVAR=0 to disable it, and I'd use == (numeric) instead of eq (string) comparisons: $?=254 if $ENV{SSH_CONNECTION} && $?==255;

      Regards,
      -- Hauke D

Re^3: 255: perl compilation error code and ssh cannot connect code
by i5513 (Pilgrim) on Nov 02, 2016 at 08:20 UTC
    A big thank for your reply !