Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Using eval: $@ isn't returning the error I expect

by haukex (Archbishop)
on Feb 20, 2020 at 00:05 UTC ( [id://11113206]=note: print w/replies, xml ) Need Help??


in reply to [SOLVED] Using eval: $@ isn't returning the error I expect

I see a couple of issues here:

  • pryrt pointed out that you're using eval BLOCK syntax with a single string inside the block, that's basically like writing "use cPanelUserConfig;" in your code, i.e. it has no effect, and warnings would have told you "Useless use of a constant ("use cPanelUserConfig;") in void context". Always Use strict and warnings!
  • haj pointed out that unless($@){use cPanelUserConfig} will always attempt to load the module. There's the if.pm pragma, but in this case a simple eval "use cPanelUserConfig;" (that's an eval STRING) would be enough to either load the module or not.
  • The pattern of checking $@ after an eval is unfortunately common, but it has issues: Bug in eval in pre-5.14.

All in all, since it sounds like you only need to load the module or not, and not actually use any of its functions (?), I would have gone with: eval "use cPanelUserConfig; 1" or warn "Didn't load cPanelUserConfig: $@";.

Replies are listed 'Best First'.
Re^2: Using eval: $@ isn't returning the error I expect
by pryrt (Abbot) on Feb 20, 2020 at 02:35 UTC
    need to load the module or not, and not actually use any of its functions (?)

    cPanelUserConfig sets up library paths for modules installed using the cPanel interface -- basically a series of use lib ...; necessary if the script uses any non-core modules that you've installed on your shared hosting server

      Exactly. The script in question uses a module I installed on the shared server, so cPanelUserConfig must be used for the script to work in that environment -- but not on my local server, where all the modules are in the same place. Thanks!
Re^2: Using eval: $@ isn't returning the error I expect
by doctormelodious (Acolyte) on Feb 20, 2020 at 00:26 UTC

    Thanks, haukex. I tried this:

    eval "use cPanelUserConfig; 1" or warn "Didn't load cPanelUserConfig: $@";

    as a replacement for the explicit "use" command:

    use cPanelUserConfig;

    and it ran fine on my own machine, but failed on the host server because on that machine cPanelUserConfig is required in order that a different module be loaded after cPanelUserConfig is loaded. (They treat user-installed modules differently from modules that are part of their default setup.) It would seem that the "eval" command isn't actually loading the cPanelUserConfig module on the host server. Only the explicit "use" statement seems to work there.

      It would seem that the "eval" command isn't actually loading the cPanelUserConfig module on the host server. Only the explicit "use" statement seems to work there.

      If it's not showing a warning, then it is loading it, but there's a difference: eval STRING delays the execution of the use until runtime, while normally it would run at compile time. Try adding a BEGIN { ... } block around the whole line, that'll move the execution back into compile time (see BEGIN in perlmod).

        BINGO!!

        I moved it to the BEGIN block that was already in place, and that did the trick.

        Thanks mucho!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113206]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found