* Philippe Bruhat (BooK) <[EMAIL PROTECTED]> [2007-10-09 01:55]:
> What's wrong about this?
> 
>     eval "use My::Big::Namespace::$_;"
>         for qw( This That Munger::Fast Munger::Precise );
>     die $@ if $@;

That a) you check $@ to see if `eval` succeeded b) you do this
only once after performing multiple `eval`s. Correct:

    eval "use My::Big::Namespace::$_; 1" or die $@
        for qw( This That Munger::Fast Munger::Precise );

In general, you should *always* (**ALWAYS**!!) check the success
of an eval block by checking its return value; if you caught an
exception you will get undef, so return something true (or at
least defined) as the last thing you do within the block. Then
you can check the block’s return value to see if it returned
something legitimate.

Checking $@ is unreliable for many reasons. __DIE__ handlers,
nested evals, objects in $@ etc all provide a myriad ways to eat
$@ before the `eval` returns or make the repeated evaluation of
$@ unsafe.

(I’ve half-joked before that someone should write Unbreak::Eval.)

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle

Reply via email to