http://www.perlmonks.org?node_id=323624


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