Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Checking for PERL Modules by epoptai
in thread Checking for PERL Modules by kidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found