Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Best way to check for installed module

by jmay (Sexton)
on Apr 04, 2002 at 19:07 UTC ( #156733=perlquestion: print w/ replies, xml ) Need Help??
jmay has asked for the wisdom of the Perl Monks concerning the following question:

Scenario: I want to determine if My::Module is installed on the current system, so I can have conditional behavior based on whether it is there or not. This is not a strict dependency, so I don't want the standard behavior of "use My::Module". So far I've come up with this:
if (defined eval "require My::Module") { print "all's well\n"; } else { print "nope, figure out a workaround\n"; }

But is this likely to be too harsh? There are other conditions that could cause require to fail besides "Can't locate My/Module.pm in xxx".

Suggestions appreciated,

-Jason

Comment on Best way to check for installed module
Download Code
Re: Best way to check for installed module
by ehdonhon (Curate) on Apr 04, 2002 at 19:43 UTC
Re: Best way to check for installed module
by Juerd (Abbot) on Apr 04, 2002 at 19:44 UTC

    if (defined eval "require My::Module") {

    If the name of the module is constant, you should use eval BLOCK instead of eval STRING:

    if (defined eval { require My::Module }) {
    Apart from that, your approach is very common, and I have yet to see a better way to do it.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      The block form may be safe with require but don't fall into that trap and use the block form with "use Module"; since the perl compiler will spot the use xxx long before the wrapping code is actually executed...

      --
      $you = new YOU;
      honk() if $you->love(perl)

Re: Best way to check for installed module
by maverick (Curate) on Apr 04, 2002 at 19:47 UTC
    There are other things that could make the eval fail, syntax errors, missing the 1; at the bottom of the pm...but in each of those cases you'd probably still want to treat it as if you "didn't have it". You couldn't use a broken module anyways... :)

    As for the actually technique... That's probably what I'd use, I can't seem to think of another way off hand.

    /\/\averick
    OmG! They killed tilly! You *bleep*!!!

Re: Best way to check for installed module
by extremely (Priest) on Apr 04, 2002 at 20:47 UTC
    The canonical way from the Cookbook is simply:
    BEGIN { unless (eval "require MyModule") { warn "Eeek! no MyModule: $@"; } } BEGIN { unless (eval "use MyModule") { warn "Eeek! no MyModule: $@"; } }

    --
    $you = new YOU;
    honk() if $you->love(perl)


      Reason: (Biker) DELETE on authors request - Will I ever learn to read?

      For more information on this node visit: this

Re: Best way to check for installed module
by DigitalKitty (Parson) on Apr 04, 2002 at 20:57 UTC
    Hi Monks,

    One very simple method uses the following one-liner:

    perl -e "use IO::Socket"

    Thanks,
    -DK
      Or similarly, as I do: perl -MModule::InQuestion -e '1'.
      /me hugs perlrun ;)

      Btw, you can even check $? (in bash) programatically to see if it was successful.

      </my 1.5 cents>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://156733]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-09-24 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (243 votes), past polls