Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Checking for modules

by Anonymous Monk
on Jul 16, 2003 at 14:34 UTC ( #274839=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I check if I have a module on NT and also Unix. I know in NT I need to go into "ppm" but not sure how to find if if I have a specific module using ppm. There must be a command to check?

Replies are listed 'Best First'.
Re: Checking for modules
by edan (Curate) on Jul 16, 2003 at 14:40 UTC

    I usually do this to check if I have a module:

    perl -e 'use Module;'

    If it complains, then you don't have it. That should work even on NT.

Re: Checking for modules
by mattriff (Chaplain) on Jul 16, 2003 at 14:42 UTC
    There are lots of ways to do it, but one quick way would be:

    perl -MData::Dumper -e ''

    Data::Dumper being the module checked for in this case. That should work in UNIX, and at least works on Win ME from a DOS prompt, as well.

    - Matt Riffle

(jeffa) Re: Checking for modules
by jeffa (Bishop) on Jul 16, 2003 at 15:09 UTC
    I am strictly a Linux user, but surely perldoc will work on NT as well:
    perldoc Foo::Bar
    is the least amount of characters i can think of to see if a module is installed on the box in question.

    Ahhh ... haven't been bitten by that yet Aristotle ... but i do believe you. Maybe i should have said:

    [root@localhost /root]# cpan

    cpan shell -- CPAN exploration and modules installation (v1.71) ReadLine support enabled

    cpan> install Foo::Bar

    instead ;) (yes yes ... this won't work with ppm, but you get the gist ... just install it, chances are if it is already installed, it could use an upgrade).

    Thanks once again Aristotle (he watches my back ;))


    (the triplet paradiddle with high-hat)
      This doesn't work if there's no POD in the module; what's worse, you get the same error regardless of whether the module is missing POD or missing entirely.

      Makeshifts last the longest.

Re: Checking for modules
by jmcnamara (Monsignor) on Jul 16, 2003 at 16:00 UTC

    Here are some simple Unix and Windows one-liners that check whether a module is installed. They return 1 or 0 accordingly:
    (Unix) perl -le 'print 0 + eval "require $ARGV[0]"' Some::Module (Win) perl -le "print 0 + eval qq(require $ARGV[0])" Some::Module

    Or as a program with clearer output:

    #!/usr/bin/perl -wl for my $module (@ARGV) { print "$module is ", eval "require $module" ? "" : "not ", "installed."; } __END__ $ perl Parse::RecDescent Parse::RecklessDescent Parse::RecDescent is installed. Parse::RecklessDescent is not installed.


Re: Checking for modules
by nega (Scribe) on Jul 16, 2003 at 16:01 UTC
    If you wanted check from within your script/program you could try something similar to the following (which is from Chapter 12 of the Perl Cookbook):

    BEGIN { unless (eval "use $mod") { warn "couldn't load $mod: $@"; } }
    I find this useful for version checking modules and throwing out meaningful errors to users, if they're lacking a particular module.
Re: Checking for modules
by DigitalKitty (Parson) on Jul 16, 2003 at 17:46 UTC
      Well, it sure helped me! This script *rocks*! I have a bunch of websites and I'm always after information on installed modules and settings. This little sweetheart just laid it all out for me. THANK YOU! Jim aka RyuMaou
Re: Checking for modules
by blue_cowdawg (Monsignor) on Jul 16, 2003 at 18:56 UTC

    Here is a sub from a CGI script that I install on new sites that I am writing CGI for. The list of modules I check for of course changes with the requirements of what I expect to write.

    sub module_inventory { my @modules=qw/ Net::Ping Net::LDAP HTML::Template GnuPG::Fingerprint GnuPG::UserID GnuPG::Tie::ClearSign Date::Range Date::Manip Date::Calc Calendar::Simple CGI::Log CGI::LogCarp CGI::Cookie CGI::CoockieSerial CGI::EZForm CGI::FormBuilder GD CGI::Graph HTML::Calendar::Simple HTML::CalendarMonth HTML::Form HTML::TableLayout Spreadsheet::WriteExcel MIME::Lite MIME::Entity MIME::Tools MIME::Base64 Mail::Send Mail::Sendmail /; my $rstring="<table border=\"1\">"; $rstring .= Tr(th("Module"),th("Status")); foreach my $mod(sort @modules) { eval " use $mod; " ; if ($@) { $rstring .= Tr(td($mod),td(b("Not Available"),$@)); } else { $rstring .= Tr(td($mod),td("OK")); } } $rstring .="</table>"; return $rstring; }

    I wrote this thing many, many, moons ago and if I really took a hard long look at it I'd probably improve on it.

    But then again... if it ain't broke... I ain't fixing it.

    Peter L. BergholdBrewer of Belgian Ales
    Unix Professional
Re: Checking for modules
by artist (Parson) on Jul 16, 2003 at 15:33 UTC
    perldoc -l Module::Name gives you the exact location of the module on Unix and NT systems.


      Not so.
      $ perldoc -l Term::Shell /usr/lib/perl5/site_perl/5.8.0/Term/Shell.pod
      It gives you the location of the POD, which may or may not be the module itself.

      Makeshifts last the longest.

        Here is what I found from perldoc at
        -l file name only
            Display the file name of the module found.
        Now, I have used it all the time. I didn't know otherwise.


Re: Checking for modules
by jmanning2k (Pilgrim) on Jul 16, 2003 at 18:47 UTC
    I have a perlwhich script (written in perl, of course), to which you can give a module name, and it will search @INC for a module by that name.
    This gives you the path to the library, and if it is installed in your @INC path or not.

    I can post it to snippets if you think it would be helpful...

    EDIT: Posted here: perlwhich - find which perl module you are using (and more). And some (better?) ones have been posted here: whichpm.
    Sample Output: >~/bin/perlwhich -m 'Storable' /util/lib/perl5.8/lib/5.8.0/alpha-dec_osf -- Storable (2.04) - persist +ence for Perl data structures /util/lib/perl5.8/lib/5.8.0 -- Memoize::Storable (0.65) - store Memoiz +ed data in Storable database
      Please post your script. thanks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://274839]
Approved by mattriff
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2018-05-21 13:21 GMT
Find Nodes?
    Voting Booth?