Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Installed modules and their versions

by cog (Parson)
on Jan 07, 2005 at 09:18 UTC ( #420210=perlquestion: print w/replies, xml ) Need Help??

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

A friend of mine asked me for a script that would show the installed modules and their versions. "Easy", I thought.

For the part of showing the installed modules, this would do the trick:

use strict;
use ExtUtils::Installed;

print for ( ExtUtils::Installed->new->modules );

As for the part of displaying the versions, it became more tricky... "Why?", many would say? "You simply load the module and print Module->VERSION or something, right?"

Wrong... what about modules that do unwanted things on startup? If you still don't know what I'm talking about, take a look at modules such as Acme::Bleach, Acme::emcA and so on...

Any ideas?

Replies are listed 'Best First'.
Re: Installed modules and their versions
by Fletch (Chancellor) on Jan 07, 2005 at 09:27 UTC

    Use CPAN?

    use CPAN (); for my $mod (CPAN::Shell->expand("Module","/./")){ next unless $mod->inst_file; my $ver = $mod->inst_version print $mod->id, ":\t", defined $ver ? $ver : "UNDEF", "\n"; }
      Hmm. I've never played around with 'use CPAN'... but wouldn't that miss modules built locally?
Re: Installed modules and their versions
by Corion (Pope) on Jan 07, 2005 at 09:28 UTC

    Do what does, and grep for the first line containing the string $VERSION, and eval that line. Or, if you eschew eval, there is a new module in the PPI namespace that tries to replicate the eval needed for

Re: Installed modules and their versions
by r34d0nl1 (Pilgrim) on Jan 07, 2005 at 10:54 UTC
Re: Installed modules and their versions
by zentara (Archbishop) on Jan 07, 2005 at 14:44 UTC
    I don't delve into this much, and may not appreciate the fine points of your question, but how about letting ExtUtils::Installed get the version, instead of loading the module?. Here is an previous snippet from perlmonks on this subject.
    #!/usr/bin/perl use ExtUtils::Installed; my $instmod = ExtUtils::Installed->new(); foreach my $module ($instmod->modules()) { my $version = $instmod->version($module) || "???"; print "$module -- $version\n"; }

    I'm not really a human, but I play one on earth. flash japh
      It works, I like it. I'm going to use that Monday!

      Bill / N1VUX

Re: Installed modules and their versions
by theorbtwo (Prior) on Jan 10, 2005 at 18:16 UTC

    Various methods of installing modules create metadata on what modules are installed. Unfortunately, reading this metadata only tells you about modules installed in this way, and there are many ways to install modules -- rpm, deb, MakeMaker, qpkg, MSI, cp...

    Don't attempt to read any of that metadata. Instead, just look and see what modules are there:

    use File::Find; use ExtUtils::MakeMaker; find( sub { next unless /.pm$/i; my $mod = $File::Find::name; $mod =~ s/^$File::Find::topdir\/?//; $mod =~ s/\.pm$//i; $mod=~s!/!::!g; my $ver=MM->parse_version($File::Find::name); print "$mod ($ver)\n" }, @INC )
    (BTW, the ExtUtils::MakeMaker call there /does/ look in the actual file (and thus does not fall prey to my problem with metadata), and /does/ run code out of it -- but only one line. Also note that it returns the string 'undef' when there is no version, and not an actual undef.)

    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2021-05-06 03:59 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (69 votes). Check out past polls.