Pregunta

Is there an equivalent of the following -

eval { die "reason 1"; }; warn $@ if $@;
eval { die "reason 2"; }; warn $@ if $@;
eval { die "reason 3"; }; warn $@ if $@;
.
.

As you can notice, the following code wont print out every possible reasons for the script to die..

eval {
    die "reason 1";
    die "reason 2";
    die "reason 3";
};
warn $@ if $@;

[EDIT] I would like to know all possible reasons the script (that uses lot many libraries) can fail. The die statements are not in a place i can edit.

¿Fue útil?

Solución

overriding die is not exactly an alternative to eval, but this is what I think you're asking about, trapping die and turning it into a warn, log die and resume/continue program

$ perl -e " eval{die 1;}; die 2; die 3; "
2 at -e line 1.

$ perl -Mwarnerous  -e " eval{die 1;}; die 2; die 3; "
FAKE die : 2 at -e line 1
FAKE die : 3 at -e line 1

$ cat warnerous.pm
*CORE::GLOBAL::die = sub {
    unless( $^S ){
        warn( qq{FAKE die : @_ @{[sprintf q{at %s line %s },(caller)[1,2] ]}\n} );
    }
};
1;

Otros consejos

Do you mean something like this?

my $problems;
for my $r (1 .. 3) {
    eval { die "reason $r"; 1 } or $problems .= $@;
}

warn "There were the following problems:\n$problems";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top