2010-09-10 20 views
12

Mi sembra di ricordare che non è sicuro fidarsi del valore di [email protected] dopo un eval. Qualcosa su un gestore di segnale che ha la possibilità di impostare [email protected] prima di vederlo o qualcosa del genere. Sono anche troppo stanco e pigro in questo momento per rintracciare la vera ragione. Quindi, perché non è sicuro affidarsi a [email protected]?

+3

Vedere anche http://stackoverflow.com/questions/503189/is-object-oriented-exception-handling-in-perl-worth-it, http://stackoverflow.com/questions/2165161/whats-broken -about-exceptions-in-perl, http://stackoverflow.com/questions/2439966/do-you-use-an-exception-class-in-your-perl-programs-why-or-why-not – Ether

+0

Nota come Perl 5.14, [Questo è stato corretto.] (http://perldoc.perl.org/perl5140delta.html#Exception-Handling) –

risposta

17

Il Try::Tiny perldoc ha la discussione definitiva del problema con [email protected]:

There are a number of issues with eval.

Clobbering [email protected]

When you run an eval block and it succeeds, [email protected] will be cleared, potentially clobbering an error that is currently being caught.

This causes action at a distance, clearing previous errors your caller may have not yet handled.

[email protected] must be properly localized before invoking eval in order to avoid this issue.

More specifically, [email protected] is clobbered at the beginning of the eval, which also makes it impossible to capture the previous error before you die (for instance when making exception objects with error stacks).

For this reason try will actually set [email protected] to its previous value (before the localization) in the beginning of the eval block.

Localizing [email protected] silently masks errors

Inside an eval block die behaves sort of like:

sub die { 
     [email protected] = $_[0]; 
     return_undef_from_eval(); 
} 

This means that if you were polite and localized [email protected] you can't die in that scope, or your error will be discarded (printing "Something's wrong" instead).

The workaround is very ugly:

my $error = do { 
     local [email protected]; 
     eval { ... }; 
     [email protected]; 
}; 

... 
die $error; 

[email protected] might not be a true value

This code is wrong:

if ([email protected]) { 
     ... 
} 

because due to the previous caveats it may have been unset.

[email protected] could also be an overloaded error object that evaluates to false, but that's asking for trouble anyway.

The classic failure mode is:

sub Object::DESTROY { 
     eval { ... } 
} 

eval { 
     my $obj = Object->new; 

     die "foo"; 
}; 

if ([email protected]) { 

} 

In this case since Object::DESTROY is not localizing [email protected] but still uses eval, it will set [email protected] to "".

The destructor is called when the stack is unwound, after die sets [email protected] to "foo at Foo.pm line 42\n", so by the time if ([email protected]) is evaluated it has been cleared by eval in the destructor.

The workaround for this is even uglier than the previous ones. Even though we can't save the value of [email protected] from code that doesn't localize, we can at least be sure the eval was aborted due to an error:

my $failed = not eval { 
     ... 

     return 1; 
}; 

This is because an eval that caught a die will always return a false value.

+1

Sì, penso che sia quello che stavo ricordando. –

+2

Non devi più giocare a quei giochi folli. – tchrist

7

[email protected] ha gli stessi problemi che ogni variabile globale è: quando qualcos'altro imposta, è resettato attraverso l'intero programma. Qualsiasi eval potrebbe impostare [email protected]. Anche se non vedi uno eval nelle vicinanze, non sai chi potrebbe chiamarne uno (subroutine, variabili legate e così via).

Problemi correlati