Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

checking for all packages

by Wiggins (Hermit)
on May 16, 2014 at 17:45 UTC ( [id://1086368]=perlquestion: print w/replies, xml ) Need Help??

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

This is a vague request that search couldn't help with. I am sure you will be able.

I want to create a 'here' document/program that will determine ALL of the missing packages from a set of specified packages in 1 run. Not the first only, per run. This is a pre-test in an installation script.

Simple. I will assume that if the named package is present, the dependencies are also loaded.

--Solution--

The replies I received were just what I was hopeing for. 4 very different approaches.

For my usual projects, I try to support the largest number of OS default installations. That means that I avoid using non-Core packages. Two of the solutions provided involve non-core packages, which will not be present in a default install, or possibly in one where additional packages requested were not installed.

That leaves two remaining solutions. The first involved a loop arround an 'eval', which is pretty much what I expected. The second was a nesting of 'map' 'grep' 'map' eval require. Both solution worked (on a core only Perl install) and supplied approximately the same information.

Using the HOP principal of being wary of 'extreme cleverness' (for future maintenance reasons), I choose the classic "looped eval require" solution

Thank you all, I will be digging into the one liner for my own edification.

It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Replies are listed 'Best First'.
Re: checking for all packages
by philipbailey (Curate) on May 16, 2014 at 19:50 UTC

    There's no doubt some more sophisticated way of doing this, but I'd probably just try to require each package in turn in a string eval. This should give you the idea:

    #!/usr/bin/perl use strict; use warnings; my @packages = qw/ Net::IP Net::CIDR List::BinarySearch /; for my $package (@packages) { eval "require $package"; if ($@) { print "Couldn't find package '$package'\n"; } else { print "Found package '$package'\n"; } }
Re: checking for all packages
by fishmonger (Chaplain) on May 16, 2014 at 20:23 UTC
Re: checking for all packages
by morgon (Priest) on May 17, 2014 at 08:51 UTC
    my @missing_modules = map { $_->[0] } grep { ! $_->[1] } map { [$_, eval "require $_"] } @wanted_modules;
Re: checking for all packages
by vinoth.ree (Monsignor) on May 16, 2014 at 18:30 UTC

    Hi,

    This is what I understand, that your are doing some installation and have a set of package list with you, and want to check those packages already installed or not?

    If any of those package is not already installed you need that package name as well as its dependency packages also. Correct me if I am wrong.

    Show us what is your progress so for on this? so that we can help you better.


    All is well
      Quickly this morning I simply wrote out a file to temp with all of the non-core Perl modules that will be used.
      use Net::IP; use Net::CIDR; use List::BinarySearch;
      then
      $rc = `perl -c /tmp/perltest.pl`;
      But this will fail on the 1st missing module and not check the next 2. I am hoping there is a 'pattern' or typical usage with 'eval', a 'foreach' and a list of module names.
      Plagiarism is the highest form if flattery.

      It is always better to have seen your target for yourself, rather than depend upon someone else's description.

        Given an array of module names, you can use UNIVERSAL::require to check for their presence:
        require UNIVERSAL::require; my @modules_to_check = qw(Net::IP Net::CIDR List::BinarySearch); for my $module (@modules_to_check) { $module->require or warn "$module failed to load\n"; }

        Hi,

        Instead of using those modules with use, you can try with perldoc command (check perldoc already installed) You can check for a module's installation path by: perldoc -l ourmodule::name

        Have it list of packages in array, use foreach loop and execute perldoc command and check. perldoc -l will return the only the file name of the module found.

        Getting the CPAN dependency tree of a Perl module

        All is well
Re: checking for all packages
by Anonymous Monk on May 17, 2014 at 02:37 UTC
    Why not do that with standard Makefile.PL dance?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1086368]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found