Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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.

Replies are listed 'Best First'.
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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://323624]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-08-17 23:40 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (294 votes). Check out past polls.