Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How do I know if a package is on my server?

by gsd4me (Beadle)
on Aug 26, 2017 at 11:06 UTC ( #1198056=perlquestion: print w/replies, xml ) Need Help??

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

Dear all,

A bit of a question, not really very difficult to you experts, but I'd like to know how the Perl package distributions work.

I have developed a script, which works using the required "include, use, require" packages and modules as necessary, uploaded it and it is in use. Which got me thinking ...

If I developed a script on my home laptop, needed some sort of "unusual" package for a particular reason, found it in CPAN, downloaded and installed it on my laptop, tested it using "localhost" methods and was happy with the result, then went to put the script on my hosting server (resident somewhere in California I believe), how do I know that the "unusual" package is available on that server - or on *any* server if truth be told?

So how do these things work? Does every server have every Perl package ever written residing on it? And what about deprecated pacakges? Or if the unusual::unique module is not on the server, how would anyone request it to be so? And what happens if you use more than one server for that package? And how would I go about seeing if the package/module *is* on the server using some script?

Just curious!
  • Comment on How do I know if a package is on my server?

Replies are listed 'Best First'.
Re: How do I know if a package is on my server?
by afoken (Canon) on Aug 26, 2017 at 11:11 UTC
    how do I know that the "unusual" package is available on that server
    perldoc perllocal
    perl -MSome::Exotic::Module -e 'print "ok"'
    perl -MSome::Exotic::Module -e 'print $Some::Exotic::Module::VERSION'

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How do I know if a package is on my server?
by shmem (Chancellor) on Aug 26, 2017 at 12:41 UTC
    And how would I go about seeing if the package/module *is* on the server using some script?

    Easy - try to use the module, if that fails, it is not there. At least it is not found in any path included in perl's built-in array @INC.

    So how do these things work?

    What kind of "hosting server" do you have? what kind of access? Is it a server with shell access (i.e. ssh) or a plain web server?

    Perl comes with many modules included, two of them are Config and Module::CoreList. The latter can be used to list all core modules:

    # command line $ perl -lE 'use Module::CoreList; say for Module::CoreList->find_modul +es' # CGI #!/usr/bin/perl use Module::CoreList; use 5.10.0; say "Content-type: text/html\n"; say <<EOH; <html><head><title>CoreList</title></head> <body><h1>Perl Core Modules:</h1> <pre> EOH say for Module::CoreList->find_modules; say "</pre></body></html>";

    The Config module exports the hash %Config which holds all information about how perl was built, including paths for module inclusion search (which, again, show up in @INC). The relevant keys are

    • privlib
    • vendorlib
    • sitelib

    In a sane setup, the core modules root is $Config{privlib}. All vendor provided modules of the platform live in vendorlib, the modules specific for the site are in sitelib.

    You can prepend an arbitrary directory to @INC using the lib module

    use lib '/path/to/my/modules';

    which ensures that modules are first looked for in there.

    This bunch of information should be enough to enable you to answer the rest of your questions by yourself.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: How do I know if a package is on my server?
by roho (Canon) on Aug 26, 2017 at 12:16 UTC
    You can also do it this way and let Perl tell you the current version (Note the use of double quotes, required for Windows):
    perl -e "use DateTime 99999" DateTime version 99999 required--this is only version 1.42 at -e line +1, <DATA> line 1.

    "It's not how hard you work, it's how much you get done."

Re: How do I know if a package is on my server?
by poj (Abbot) on Aug 26, 2017 at 12:29 UTC

    If you have a web server ..

    #!/usr/bin/perl # moduletest.cgi use strict; my @mod = qw( Mail::Sendmail CGI DBI Unusual ); print "Content-type:text/plain\n\n MODULE TEST ===========\n\n"; for (@mod){ eval ("use $_"); if ($@){ print "$_ \nERROR - $@\n"; } else { my $v = $_->VERSION; print "$_ \nOK - Version $v\n"; } print "-" x 80,"\n"; }
    poj
Re: How do I know if a package is on my server?
by Your Mother (Bishop) on Aug 26, 2017 at 13:52 UTC

    You got plenty of good answers. Something I also use to explore these issues isĖ

    moo@cow[3]~>perldoc -l Moo /usr/local/lib/perl5/site_perl/5.18.2/Moo.pm

    Itís especially useful in a modified environment where you might have multiple versions of things and your code is locating/using unexpected versions.

      perldoc -lm is better than perldoc -l because it also works for modules without POD.

Re: How do I know if a package is on my server?
by haukex (Chancellor) on Aug 27, 2017 at 10:35 UTC

    I'm a bit late to the TIMTOWTDI party, but here's my contribution: Module::Load::Conditional's check_install with Data::Dumper (both are core modules):

    use Module::Load::Conditional qw/check_install/; use Data::Dumper; print Dumper( check_install( module => 'Some::Module' ) );
    So how do these things work? Does every server have every Perl package ever written residing on it?

    No, but: Perl comes with a set of "core" modules, as the other Monks have mentioned. Some OS distributions actually strip out some of these core modules, but in my experience on web servers, that's rare - so you can almost always rely on these core modules being available. In addition, many distributions of *NIX OSes will include a bunch more Perl modules, those required by the other packages installed on the machine. (Or, in the case of Windows, pre-packaged Perl distributions like Strawberry Perl will include a bunch of modules often needed by users - example.)

    In the case of *NIX OSes, this initial set of modules will usually be installed via the system's package manager, like apt or yum. System administrators then have the option of installing additional modules either via the package manager or via tools like cpan or cpanm (although using those to install modules into the system Perl is not something I'd recommend, but that's a topic for another post). System admins also have the option of installing whole different versions of Perl, in case their users need them. For example, Pair, which hosts this site, provides at least 5.10.1, 5.12.2, 5.14.1, 5.16.2, and 5.22.1 and a ton of pre-installed modules, most likely based on popular demand by users (Update: I should also mention that typically each Perl installation will get its own independent set of directories into which it installs its modules). Installing modules into those custom-built Perls is then usually done via the aforementioned cpan/cpanm tools.

    Or if the unusual::unique module is not on the server, how would anyone request it to be so?

    In the case of web servers, you can probably try submitting a support ticket, and often ISPs will consider installing additional modules system-wide if they are requested by enough users. Otherwise, if they won't do it for you, it depends a bit on what kind of access you have to the web server. If you have shell (ssh) access, there are ways to install modules into your home directory either by hand or using a tool like local::lib, so that they are available only to your user and your website. If you don't have shell access (FTP-only or whatever), things are a little more tricky - but for Perl modules that are pure-Perl, i.e. they have no C/XS components that need to be compiled on the server, often it is still possible to get them onto the server, but that's also a topic for another post.

Re: How do I know if a package is on my server?
by stevieb (Abbot) on Aug 26, 2017 at 20:13 UTC

    Great responses so far from the Monks, but I didn't see cpan's autobundle mentioned, so here it is, in case you find it useful.

    The following command will bundle all of your installed distributions into a single module file that you can then transfer over to any other system, and unbundle it, which installs all of those modules on the new system. It keeps versions and everything.

    perl -MCPAN -e autobundle

    Output displays all of the modules as it bundles them, then at the end upon success:

    Wrote bundle file /home/spek/.cpan/Bundle/Snapshot_2017_08_26_00.pm

    You can then back up, or transfer that file to another system, and install all of those modules with a single command. Copy the file into your .cpan/Bundle directory on the new system, wherever that may be. Note the lack of the .pm file extension here:

    perl -MCPAN -e 'install Snapshot_2017_08_26_00'
Re: How do I know if a package is on my server?
by kcott (Bishop) on Aug 26, 2017 at 17:31 UTC

    G'day gsd4me,

    You seem to have answers to your questions. I might just also point you to corelist, which is a front-end utility to Module::CoreList (as discussed by ++shmem).

    Although you only asked about modules on different servers, there's also the issue of modules used by different versions of Perl on the same server. I have ten different versions of Perl installed: I often want to check whether a particular Perl version has a module installed and, if so, what that module's version is.

    Here's how I've set things up to handle this. My default shell is bash. My functions, which I source in "$HOME/.bash_profile", are in "$HOME/.bash_functions", which contains this "perlmodver" function:

    $ cat ~/.bash_functions ... perlmodver () { for i in "$@"; do eval "echo \`perl -E 'no warnings q{deprecated}; require $i; s +ay qq{$i }, \$$i::VERSION'\`" done } ...

    Example usage:

    $ perlmodver strict DBI Bogus strict 1.11 DBI 1.636 Can't locate Bogus.pm in @INC ...

    Depending on your setup, that may work as is; may require minor modification; or might need a major rewrite.

    I use it often enough that it's a useful function to have in my toolbox. If this is something you'll use rarely, one of the command line options (already shown) may be a better choice.

    — Ken

Re: How do I know if a package is on my server?
by Perlbotics (Chancellor) on Aug 26, 2017 at 18:13 UTC

    Perhaps, it is useful to see which files were included during execution, in order to have a reference?

    use strict; use warnings; #-- example use Getopt::Long; #-- example / somewhere in your code or imported modules require Math::BigInt; #-- ... more ... #-- at the end, we should have seen them all END { print "VERSION / MODULE / PATH:\n"; for my $module (sort keys %INC) { my $mod_name = $module; $mod_name =~ s{/}{::}; $mod_name =~ s/\.pm$//i; #-- perhaps, the module author followed the convention? my $ver = eval "\$${mod_name}::VERSION" || '?'; printf "%-8s %-35s %s\n", $ver, $mod_name, $INC{$module}; } }

    Example output on my host:

    VERSION / MODULE / PATH: 1.3301 Carp /opt/perl-5.18.2/lib/site +_perl/5.18.2/Carp.pm 5.70 Exporter /opt/perl-5.18.2/lib/site +_perl/5.18.2/Exporter.pm 5.70 Exporter::Heavy /opt/perl-5.18.2/lib/site +_perl/5.18.2/Exporter/Heavy.pm 2.45 Getopt::Long /opt/perl-5.18.2/lib/site +_perl/5.18.2/Getopt/Long.pm 1.9993 Math::BigInt /opt/perl-5.18.2/lib/5.18 +.2/Math/BigInt.pm 1.27 constant /opt/perl-5.18.2/lib/5.18 +.2/constant.pm 1.22 overload /opt/perl-5.18.2/lib/5.18 +.2/overload.pm 0.02 overloading /opt/perl-5.18.2/lib/5.18 +.2/overloading.pm 1.07 strict /opt/perl-5.18.2/lib/5.18 +.2/strict.pm 1.03 vars /opt/perl-5.18.2/lib/5.18 +.2/vars.pm 1.18 warnings /opt/perl-5.18.2/lib/5.18 +.2/warnings.pm 1.02 warnings::register /opt/perl-5.18.2/lib/5.18 +.2/warnings/register.pm

Re: How do I know if a package is on my server?
by zakame (Pilgrim) on Aug 26, 2017 at 16:06 UTC
    how do I know that the "unusual" package is available on that server - or on *any* server if truth be told?

    For standalone scripts and apps, the "modern" approach (taken from other systems like Ruby and Python) is to bundle such scripts with a cpanfile that describes the dependencies that your scripts will need. This way, you do not have to query whether such a dependency is on your destination server or not; you declare such a dependency instead, so that tools like cpanm can install those for you.

    In addtion, should you need to depend on exact versions of a particular module, tools like Carton will read the cpanfile to get that particular version installed for your usage.

Re: How do I know if a package is on my server?
by fishy (Pilgrim) on Aug 26, 2017 at 18:40 UTC

    Testing if "This::Package" is available (installed):

    $ perl -MThis::Package -e 1

    For example:

    $ perl -MB::Deparse -e 1 $ perl -MThis::Package -e 1 Can't locate This/Package.pm in @INC (you may need to install the This +::Package module) (@INC contains: /home/netbook/tmp/perl-5.24/lib/sit +e_perl/5.24.1/i686-linux-thread-multi /home/netbook/tmp/perl-5.24/lib +/site_perl/5.24.1 /home/netbook/tmp/perl-5.24/lib/5.24.1/i686-linux-t +hread-multi /home/netbook/tmp/perl-5.24/lib/5.24.1 .). BEGIN failed--compilation aborted.
    Note: if installed, no output is shown.

    Have fun!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2019-07-19 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?