Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: use module if condition is met

by ysth (Canon)
on Jan 23, 2004 at 17:34 UTC ( [id://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.

    Grygonos

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-26 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found