Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: use module if condition is met

by ysth (Canon)
on Jan 23, 2004 at 17:34 UTC ( #323624=note: print w/ replies, xml ) Need Help??


in reply to use module if condition is met

If $type is known when your script is compiling (e.g. based on operating system or information in @ARGV), use the if module for this:

use if $type eq 'x', "Some::Module";
(Your try at if ($type eq 'x') { use Some::Module } doesn't work because the use will be performed at compilation time, before the check of $type even happens.)

Otherwise, use some form like eval "use $module" or eval { require $module; $module->import; } (but not eval { use $module }, because there the use is again performed at compilation time).

Having the use happen at run-time is not ideal, because there can be differences in how your code will compile; e.g.:

$perl -we'eval "use Errno qw/EINVAL/"; print EINVAL' Name "main::EINVAL" used only once: possible typo at -e line 1. print() on unopened filehandle EINVAL at -e line 1. $perl -we'use Errno qw/EINVAL/; print EINVAL' 22
To detect if a subroutine exists, use exists(&subname). This will not detect subs that would be AUTOLOADed, unless they have a forward declaration (e.g. sub Foo;). defined(&subname) is slightly different and probably not what you want; see perldoc defined and exists.


Comment on Re: use module if condition is met
Select or Download Code
Re: Re: use module if condition is met
by Grygonos (Chaplain) on Jan 24, 2004 at 02:41 UTC
    Thanks monks, the if module worked perfectly. I simply passed my client name as $ARGV[0] and it works.

    Grygonos

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2015-07-01 23:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls