2010-09-10 30 views
12

eval'dan sonra [email protected] değerine güvenmenin güvenli olmadığını hatırlıyorum. Sinyal işleyicisinin, bir şey görmeden önce [email protected]'u ayarlama şansı olan bir şey. Ben de gerçek sebebi bulmak için şu anda çok yorgun ve tembelim. Peki, neden [email protected] güvenmek güvenli değil?

+3

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

+0

Not Perl 5.14 gibi, [Bu düzeltildi.] (http://perldoc.perl.org/perl5140delta.html#Exception-Handling) –

cevap

17

Try::Tiny perldoc [email protected] ile sorun kesin bir tartışma vardır. Herhangi bir eval, [email protected]'u ayarlayabilir. Yakında bir eval görmüyorsanız bile, başka birini arayabileceğinizi bilmiyorsunuz (altprogramlar, bağlı değişkenler vb.).

+1

Evet, hatırladığım şey bu. –

+2

Artık o deli oyunları oynamak zorunda değilsiniz. – tchrist

13

Try::Tiny docseval/[email protected] eksikliklerinin oldukça iyi bir listesine sahiptir. Sanırım orada Localizing [email protected] silently masks errors bölümüne başvurabilirsiniz. başka bir şey koyduğunda, bu tüm program boyunca reset oluyor:

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.