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

Hi, I have several applications using a function
sub DieWithCode { my $numerical_exit_code = shift; my $msg = shfit; # do something with $msg (inserting application name, linebreaks... i +f necessary exit ($numerical_exit_code); }
worked fine also when I packed the application using pp (PAR::Packer) into a self-extracting binary. Both under Windows (Strawberry Perl 5.22 ) and Linux (5.22 too). Then I moved the essential part of the code of one application into a sub into a module. Let's call this sub AppAction (parameters) Still everything worked fine. But: In another interactive application (command line, shell like) I want to execute this function WITHOUT the whole program exiting in case of an error where the application packed into the sub would normally exit.
OK.
Changed DieWithCode to
sub DieWithCode { my $numerical_exit_code = shift; my $msg = shfit; # do something with $msg (inserting application name, linebreaks... i +f necessary if ($^S) { $EXITCODE = $numerical_exit_code; # module global var die ($msg); } print $msg; exit ($EXITCODE); }
So the function would die in case of
eval { AppAction (myParameters)}
and exit when I embed the call into a standalone program/script.
So when I call it using eval:
my $e; { local $@; my $result = eval { AppAction (\%opt ) }; if (!defined $result) { # died... $e = $@; printf "result : %s, e : %s\n", defined $result?sprintf "%d", +$result: 'undef', $@; } chomp $e; } printf "AppAction failed ($e)\n" if $e; $EXITCODE = 0;

I can see AppAction dying, being caught by eval and the main (interactive app) would continue
regardless of executing perl mainapp.pl or execute ./main_app (binary)
BUT: When I call AppAction as a function in the standalone version:
AppAction (%opt);
there's a difference
perl mainapp.pl exits (correctly)

but

./main_app dies, as if AppAction would have been eval'ed.

SO MainApp would stop/die Can somebody explain this? And/or give me some hints how to correct this.

I already had a look into PAR.pm but could not see that the packed script is eval'ed.
The main reason I came across his strange behavior of the packed binary is:
In a GNU Make file I added a target for testing the packed binary.
Doing some basic invocations, like -? for help or a simple action that also exits with EXITCODE=0 the
make is stopping after the first invocation, due to errorlevel 255 (set by perl as result code for a die)
even when I set the exitcode $? in an END block to 0.
When I change the execute BINARY parameters to a perl APP_SCRIPT.pl parameters everything works fine...
This is the second strange thing happening
Thanks Axel

Replies are listed 'Best First'.
Re: PAR::Packer/pp and die/exit
by Anonymous Monk on Apr 01, 2020 at 08:34 UTC