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

Checking for PERL Modules

by kidd (Curate)
on Jul 19, 2002 at 18:39 UTC ( #183364=perlquestion: print w/replies, xml ) Need Help??

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


I was wondering if someone could tell me how can I do this:
I want to make a script wich would check if a user has certain modules installed to see if they can use my scripts...

I know that there is CGI::FatalToBrowsers(I think you write it like this) but it gives the warning in english, and I want to modify the messages to be in other language and less technichal...


Replies are listed 'Best First'.
Re: Checking for PERL Modules
by epoptai (Curate) on Jul 19, 2002 at 18:47 UTC
    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='$_'>$_</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.

Re: Checking for PERL Modules
by jwest (Friar) on Jul 19, 2002 at 18:50 UTC
    What you want to do is try to load the module in the standard Perlish way, trapping exceptions. An eval block will trap exceptions. The messages that these exceptions produce will be in $@. So, try this:

    my @module = qw/Foo Bar::Baz Some::Other::Module/; for my $module (@module) { eval { require $module; }; warn "$module is not available" if $@; }

    Suggested reading includes eval, require, and use. For homework, you can adapt this code to include version checking. Read up on Exporter for that.

    Hope this helps!

    Oh- by the by, 'Perl' is preferred over 'PERL'.


    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
Re: Checking for PERL Modules
by Abstraction (Friar) on Jul 19, 2002 at 18:46 UTC
    You can specify your own error message like so(from the CGI::Carp docs):
    use CGI::Carp qw(fatalsToBrowser set_message); BEGIN { sub handle_errors { my $msg = shift; print "<h1>Oh gosh</h1>"; print "Got an error: $msg"; } set_message(\&handle_errors); }

    This will catch all errors and this may not be the desired behavior.

    More on CGI::Carp
Re: Checking for PERL Modules
by jlf (Scribe) on Jul 19, 2002 at 18:48 UTC
    Try using eval and require:

    # $module is the module you're checking for print "module $module is ", eval("require $module") ? "present\n" : "absent\n";
      WOW!!! That seems easy...thanks for your replies, that was exactly what I was looking for...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2021-01-15 15:43 GMT
Find Nodes?
    Voting Booth?