Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Base script behavior on presence (or non-) of a module

by tobyink (Canon)
on Dec 21, 2011 at 16:29 UTC ( [id://944659]=note: print w/replies, xml ) Need Help??


in reply to Base script behavior on presence (or non-) of a module

I strongly suggest using Class::Load to handle this, but if you'd rather homebrew a solution...

if (eval { require Fictitious::Module; 1 }) { # code that uses Fictitious::Module } else { warn "Can't find Fictitious::Module. Soldiering on regardless..."; }

Replies are listed 'Best First'.
Re^2: Base script behavior on presence (or non-) of a module
by Anonymous Monk on Dec 21, 2011 at 16:54 UTC

      It's perhaps worth pointing out that those weren't core modules in 5.8.x.

      To me, it sounds like the OP is targetting a wide range of installations, trying to settle on the "lowest common denominator". So there might be 5.8 installations among them.

      In other words, in order to not have the program die with "Can't locate Module/Load.pm in @INC ..." on 5.8 installations, the OP would have to first check the classic "built-in" way, i.e. BEGIN { eval {require Module::Load; 1} or ...}, if the module itself is available — and depending on the outcome, possibly do the loading of the other non-core modules the same classic way...

      Just a thought.

        Yeah

        Module::Pluggable was first released with perl v5.8.9

        Module::Load was first released with perl v5.9.4

        Both are available from CPAN :)

      Module::Pluggable does an entirely different job to Class::Load - it searches for (and optionally requires and instantiates) modules with names matching a particular pattern.

      Module::Load does something similar to Class::Load but has less functionality. Class::Load not only provides load_class (equivalent to Module::Load's load function), but also try_load_class and is_class_loaded, load_first_existing_class and load_optional_class which can be pretty handy at times.

      (The difference between try_load_class and load_optional_class is subtle. Both will check whether the class exists, attempt to load it, and return a boolean indicating successfulness. The difference is in how they handle the situation where the module exists, but cannot be loaded - e.g. due to syntax errors - the former just returns false; the latter croaks.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found