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

Hi Monks!
I have a program that goes into a specific directory and list all my Perl files with its CVS version and also all the Perl modules used by the scripts. I would like to also include the modules "version" it finds, like if my Perl file uses XML::DOM, print the version like: "XML::DOM v1.44".
I found this code that can be a good start. My question is how could I pass a Perl module at the time to this code to get the modules's version printed on the screen instead of printing all the modules it finds?
Here is the code:
#!perl use strict; use warnings; #use feature qw(:5.12); use ExtUtils::Installed; use Module::CoreList; use Module::Info; my $inst = ExtUtils::Installed->new(); my $count = 0; my %modules; foreach ( $inst->modules() ) { next if m/^[[:lower:]]/; # skip pragmas next if $_ eq 'Perl'; # core modules aren't present in this +list, # instead coming under the name Perl my $version = $inst->version($_); $version = $version->stringify if ref $version; # version may be r +eturned as # a version object $modules{$_} = { name => $_, version => $version }; $count++; } foreach ( Module::CoreList->find_modules() ) { next if m/^[[:lower:]]/; # skip pragmas my $module = Module::Info->new_from_module($_) or next; $modules{$_} = { name => $_, version => $module->version // q(???) + }; $count++; } foreach ( sort keys %modules ) { print "$_ v$modules{$_}{version}\n"; } print"\nModules: $count\n"; __END__

Thanks for the Help!

Replies are listed 'Best First'.
Re: Perl modules Version Help!
by kcott (Archbishop) on Nov 10, 2010 at 15:11 UTC

    I've assumed you meant "... pass a Perl module at a time ...". If that's right, this does what you want using Module::Info:

    #!perl use 5.12.0; use warnings; use Module::Info; while (my $module = <>) { chomp $module; my $mod_info = Module::Info->new_from_module($module); if (! $mod_info) { warn qq{Can't find module: $module\n}; next; } say join q{ v}, $module, $mod_info->version(); }

    Here's some sample output:

    $ version_prob.pl Carp Carp v1.15 Scalar::Util Scalar::Util v1.23 Module::Info Module::Info v0.32 blah Can't find module: blah Data::Dumper Data::Dumper v2.126 <Ctrl-D>

    -- Ken

      Why it needs to use "use 5.12.0;". It would not work with other versions?

        Change 5.12.0 to strict and say to print (and append a newline) if you want.

        -- Ken

Re: Perl modules Version Help!
by cdarke (Prior) on Nov 10, 2010 at 15:17 UTC
    To get the specific version of one module should just be a case of inspecting its $VERSION variable. Not all modules have that though, particularly the older ones (I don't know XML::DOM). Here is an example:
    require CGI; print "$CGI::VERSION\n"; print "$INC{'CGI.pm'}\n";
      I am trying to use this:
      use strict; use warnings; use ExtUtils::Installed; my $instmod = ExtUtils::Installed->new(); my $module_test="CGI qw( -oldstyle_urls :standard )"; my $version = defined($instmod->version($module_test)) || "???"; print "$module_test -- $version\n";

      But if the module is not found its giving me an error:
      CGI qw( -oldstyle_urls :standard ) is not installed at modules.pl

      How can that be, shouldn't it be default to the "???" ?

        What module is CGI qw( -oldstyle_urls :standard ) supposed to be? Do you mean CGI? If so, then ask for just CGI.