http://qs321.pair.com?node_id=1210483


in reply to Re^2: Help Changing use into require/import
in thread Help Changing use into require/import

Good point.

My goal in this was to be able to use more complicated "use" statements like:

use My::Module 0.04 qw( :sub1 :sub2 );

I just can't seem to get those to work with the require / import way so needed a way to "use", which didn't blow up if the module wasn't found. I'm sure I didn't "invent" this; rather, Google-d, found something and used it without understanding the implications you pointed out. And I've just been "using" it ever since.

I wonder if "undef $@" before the 'eval' would help ensure any error ending up in $@ is coming from the eval?

Replies are listed 'Best First'.
Re^4: Help Changing use into require/import
by Discipulus (Canon) on Mar 07, 2018 at 22:03 UTC
    > I wonder if "undef $@" before the eval would help ensure any error ending up in $@ is coming from the eval?

    Better using local in such case (I think..)

    { local $@; eval "use Capture::Tiny qw(capture)"; if ( !$@ ) { $HAVE_Capture_Tiny = 1; } }

    See also eval, DESTROY, die and $@ - educational couple of hours

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Yes, localizing $@ will get rid of at least one problem for certain, where $@ is quashed if it already has something from a previous eval.

Re^4: Help Changing use into require/import
by stevieb (Canon) on Mar 07, 2018 at 22:06 UTC

    It's pretty rare that the issues I pointed out will crop up, but I thought I'd raise them here just in case.

    I hate to call it a "race condition" (because technically it isn't), but it is something to be aware of, and I found out about the 1; trick after spending hours sorting out why some of my tests were failing (a previous call to eval's error was being reset, causing all sorts of grief later on).

    Only wanted to state it here as although I don't believe it's an official "best practice" or "perl idiom", it really ought to be :)

    Besides, I explicitly don't like string eval, so avoid it wherever feasible/possible.