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

Not putting down the way you do it, but just checking $@, particularly without checking what's in it could be problematic in some cases (ie. the variable could potentially be set during this check, unrelated to your eval(), which would technically trigger a false-positive failure). Any eval error happening in the require could also fail with an error, but your eval will reset this var to undef as soon as it starts. Here's a very similar way that does kind of the same thing, but checks the state of the eval, not whether there's an error. It also uses block eval as opposed to string eval:

BEGIN { # look for JSON::XS, and if not available, fall # back to JSON::PP to avoid requiring non-core modules my $json_ok = eval { require JSON::XS; JSON::XS->import; 1; }; if (! $json_ok){ require JSON::PP; JSON::PP->import; } }

What happens there is that if any of the lines prior to the 1; in the eval fail, the eval block will return false (undef to be specific). If all expressions within the eval succeed, the $json_ok variable will be true, and we can proceed confidently.

See a number of issues with eval in the Try::Tiny documentation (which could be, after all, a different approach to the OP's problem. To be honest, I've never used said distribution that I recall).

Replies are listed 'Best First'.
Re^3: Help Changing use into require/import
by VinsWorldcom (Prior) on Mar 07, 2018 at 16:50 UTC

    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?

      > 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


      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.

      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.