Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Checking for PERL Modules

by epoptai (Curate)
on Jul 19, 2002 at 18:47 UTC ( #183370=note: print w/ replies, xml ) Need Help??


in reply to Checking for PERL Modules

You can check for the existence of a module with eval:

eval "use Some::Module"; $@ && print_an_error_message_with_a_nice_link_to_cpan();


Here's another technique for using non-core modules that fits in better with the other module calls, and is able to report multiple missing modules:

#!/usr/bin/perl -w use strict; use CGI ':standard'; use_('Some::Module'); use_('Some::OtherModule'); install_modules() if %_; # if use_ fails this happens sub use_ { eval "use $_[0]"; $@ && $_{$_[0]}++; undef $@ } sub install_modules { # link to dist(s) on cpan if(keys %_ > 1){ $_ = 's' } else { $_ = '' } my $p = header; $p .= qq~Required perl module$_ not found: ~; for(keys %_){ $p .= qq~<a href='http://search.cpan.org/search?dist=$_'>$_</a>, ~ +; } $p =~ s|, $||; print $p; exit }
Hope this helps.

Update: Cleaned it up a bit.

--
Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.


Comment on Re: Checking for PERL Modules
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2015-07-07 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls