I have this code currently in a part of my perl script (working fine) :
try {
no strict 'refs';
require_module $modules{$action_name};
if ( &{"FFTG::${actiontype}::run"}( $action, $fs, $log ) ) {
$log->res("SUCCESS");
} else {
if ( $action->{mandatory}[0] == 1 ) {
$log->res("FAIL, exiting.");
last ACTION;
} else {
$log->res("FAIL, but not mandatory, continuing..");
}
}
}
catch {
$log->res("MODULE NOT FOUND");
print "no module found for action '$actiontype', unable to process this action\n";
#warn $_;
};
but unfortunately, the output of the script shows these 3 errors in the middle of the output (all lines beginning with Exiting subroutine):
starting transfer ..
no module found for action 'Transfer', unable to process this action
no module found for action 'Transfer', unable to process this action
no module found for action 'Archive', unable to process this action
Exiting subroutine via last at ./bin/fftgv2.pl line 130.
Exiting eval via last at ./bin/fftgv2.pl line 130.
Exiting subroutine via last at ./bin/fftgv2.pl line 130.
ending transfer
even if the script works perfectly, I find it "dirty" to display these errors on the standard output.
is there a way to get rid of them ? (I cannot use if/then etc.. because I don't want the script to exit with this require_module thing). I just want my script to display an alert if it cannot find a module (it does), but not these warnings
thanks again regards
In perldoc perldiag we find that those warnings are in a group named "exiting".
If you used no warnings "exiting"; just prior to line 130 in fftgv2.pl, you would silence the warnings for the remainder of that block.
... of course paying attention to the warnings wouldn't be the worst idea either.
The warnings are issued because you've got a last inside the block of a try statement. The try is not actually part of the Perl syntax. It's not a keyword, and as such, the block is not really a block. It's just an anonymous sub that's treated like a block because of the prototype sub try (&;@) in Try::Tiny.
We're going to look at it from the bottom up.
Exiting subroutine via last at ./bin/fftgv2.pl line 130. ^ Exiting eval via last at ./bin/fftgv2.pl line 130. | Exiting subroutine via last at ./bin/fftgv2.pl line 130. |Exiting subroutine via last at ./bin/fftgv2.pl line 130.
So the third warning is actually from the sub for your try.
try { # here
no strict 'refs';
Exiting eval via last at ./bin/fftgv2.pl line 130.
The next one is because Try::Tiny implements try via a call to eval, Perl's built-in way of catching errors.
This is the code Try::Tiny uses (highlighted comment mine):
# failed will be true if the eval dies, because 1 will not be returned # from the eval body my $failed = not eval { # here is 2) $@ = $prev_error; # evaluate the try block in the correct context if ( $wantarray ) { @ret = $try->(); # and this is 3) } elsif ( defined $wantarray ) { $ret[0] = $try->(); } else { $try->(); }; return 1; # properly set $failed to false };
Exiting subroutine via last at ./bin/fftgv2.pl line 130.
And the first warning is from the actual sub try in Try::Tiny itself.
So what can you do about it? If you don't want to just hide the warnings, you need to rewrite your code a little. Get rid of the last inside of the try block, and instead either make it exit via a die, or simply return. Remember, it's a function after all.
Then set a value that you use later to decide if you want to skip the iteration or not.
ACTION: foreach my $action_name (@foo) {
my $break_out;
try {
no strict 'refs';
require_module $modules{$action_name};
if ( &{"FFTG::${actiontype}::run"}( $action, $fs, $log ) ) {
$log->res("SUCCESS");
}
else {
if ( $action->{mandatory}[0] == 1 ) {
$log->res("FAIL, exiting.");
return $break_out = 1; # use this to exit the loop later
}
else {
$log->res("FAIL, but not mandatory, continuing..");
}
# return is only needed if there is more code here ...
}
# ... or here
}
catch {
$log->res("MODULE NOT FOUND");
print "no module found for action '$actiontype', unable to process this action\n";
};
last ACTION if $break_out; # oops, something went wrong, break out
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With