Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re^2: using a variable to specify which perl module to use (bareword)

by William G. Davis (Friar)
on Jul 15, 2003 at 18:45 UTC ( [id://274518]=note: print w/replies, xml ) Need Help??


in reply to Re^2: using a variable to specify which perl module to use (bareword)
in thread using a variable to specify which perl module to use

Actually, no, it's not a matter of taste. It's best to avoid eval STRING whenever possible since it forces dynamic runtime recompilation, and is thus rather slow. An eval BLOCK is always preferable. If you can represent the package names as literals (barewords) within your program, use this:

if (something()) { eval { require Text::CSV; Text::CSV->import; } } else { eval { require Text::CSV_XS; Text::CSV_XS->import; } }

The eval BLOCK syntax as well allows you to catch any fatal errors, storing them in $@:

die "Couldn't load module: $@" if ($@);

Replies are listed 'Best First'.
Re^4: using a variable to specify which perl module to use (bareword)
by tye (Sage) on Jul 15, 2003 at 19:14 UTC
    It's best to avoid eval STRING whenever possible since it forces runtime recompilation, and is thus rather slow.

    Avoiding string eval is generally good advice. Always avoiding it just for speed reasons is premature micro optimization. If you were doing this in a loop and profiling showed that it was actually causing you a noticeable slow-down, then that would be a good reason to refactor the code to avoid it.

    My taste leads me to prefer much simpler code that runs a few milliseconds slower nearly every time.

    You left out the code to set what class to use when creating the object(s). If you add that in, then you are having to say each package name 3 times. I'd rather use the code I provided above. (:

    Thanks for mentioning the fatal error trapping. In this situation, eval's trapping of errors is a disadvantage. I should have mentioned that the extra step would be required if the "simpler" eval "use $module" were used. I'd just die "$@\n" if $@; so that the error message is as expected.

    Also note that you need to put the die code inside the BEGIN block in your code above.

                    - tye

Log In?
Username:
Password:

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

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

    No recent polls found