2010-09-10 21 views
12

Me parece recordar que no es seguro confiar en el valor de [email protected] después de eval. Algo sobre un manejador de señal que tiene la oportunidad de establecer [email protected] antes de verlo o algo así. También estoy muy cansado y flojo ahora mismo para rastrear la verdadera razón. Entonces, ¿por qué no es seguro confiar en [email protected]?

+3

Véase también 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 que como Perl 5.14, [Esto ha sido corregido.] (http://perldoc.perl.org/perl5140delta.html#Exception-Handling) –

Respuesta

17

El Try::Tiny perldoc tiene la discusión 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í, creo que eso es lo que estaba recordando. –

+2

Ya no tiene que jugar esos juegos locos. – tchrist

7

[email protected] tiene los mismos problemas que cada variable global tiene: cuando se establece otra cosa, pone a cero en todo el programa. Cualquier eval puede establecer [email protected]. Incluso si no ve un eval cerca, no sabe a quién más podría llamarle uno (subrutinas, variables vinculadas, etc.).

Cuestiones relacionadas