niemeand has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. I would like to use one script on different systems but can't presume the installation of different modules.

In the scenario of a special desired but not available module, the functionality of cause should not be provide.

My problem isn't to inform the script manually by setting a flag in the preferences file of this app, if module available or not, but I don't know how to let the perlinterpreter ignore concerned code by using "require" or call a procedure to an if-condition, where the "use module" -state is inside, without getting a message like:

Can't locate Image/Magick.pm in @INC ...

Many thanks in advance,
Andreas Niemeyer

  • Comment on existence check of Module and dynamic load

Replies are listed 'Best First'.
Re: existence check of Module and dynamic load
by robartes (Priest) on Feb 07, 2003 at 10:55 UTC
    Hi,

    you could simply wrap the use of your optional module in an eval block:

    eval "use Foo;"; if ($@=~/Can't locate/) { print "Module Foo is not available.\n"; }

    CU
    Robartes-

Re: existence check of Module and dynamic load
by Pardus (Pilgrim) on Feb 07, 2003 at 10:53 UTC
    So basicly you want to check for module availability on run-time ? This will work:
    eval('require Image/Magick.pm'); my $IM = $@ ? 0 : 1; #... if ($IM) { #Some Image::Magick specific code }

    --
    Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<
Re: existence check of Module and dynamic load
by niemeand (Initiate) on Feb 07, 2003 at 12:41 UTC
    Great, many thanks for help. Best regards, Andreas Niemeyer