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

Sounds like some kind of wrapper script really would be best. Am on mobile right now so I can't test if the following works for compilation errors too, but END blocks should normally allow you to change the exit code, e.g. this at the top of your script: END { $? = 254 if $? == 255 } (again, warning, untested)

Replies are listed 'Best First'.
Re^2: 255: perl compilation error code and ssh cannot connect code (updated)
by haukex (Archbishop) on Nov 02, 2016 at 08:13 UTC

    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

      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

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

      Hi i5513,

      But I did not realized about END code should go before the end of the script

      The idea is Perl needs to see the END block before it hits the error, otherwise it doesn't get queued for execution. So it's best to put the END block as early as possible. See also BEGIN, UNITCHECK, CHECK, INIT and END.

      Hope this helps,
      -- Hauke D

        Yes, I see :) Thanks !