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

I often need to find the version of different modules so I have come up with the small script to extract and print the $VERSION of a given module, since I am tired of writing one-liners to do it.
But I was wondering if there was a smarter way to get a module name, if the case where I just give a path to a .pm file?
#!/usr/bin/perl -w # $Id: version.pl,v 1.2 2003/05/14 19:44:28 jonasbn Exp $ use strict; my $modulename = shift; my $modulepath = $modulename; if ($modulepath =~ m/::/) { $modulepath =~ s[::][/]g; require "$modulepath.pm"; } else { undef $/; open(FIN, "<", $modulepath) || die ("Unable to open file $modulepa +th"); my $module = <FIN>; close(FIN); ($modulename) = $module =~ m/package (.*);/; require "$modulepath"; } my $str = '$'.$modulename.'::VERSION'; print "$modulename is version: "; print eval($str); print "\n";

Replies are listed 'Best First'.
Re: package name?
by bigj (Monk) on May 14, 2003 at 20:01 UTC
    There is a CPAN module out there Module::InstalledVersion :-)
    From the SYNOPSIS:
    use Module::InstalledVersion; my $m = new Module::InstalledVersion 'Foo::Bar'; print "Version is $m->{version}\n"; print "Directory is $m->{dir}\n";
    And not to forget (as I did in the first try), to find any information about a module, use Module::Info

    Greetings,
    Janek

      I tried Module::Info, but apparently the name retrieval when using Module::Info's new_from_file constructor does not work. I have sent two patches to the author fixing the name and dir part, but I don't think they will be accepted.
        Just to recap, My patches were not accepted by the author of Module::Info, so I have made a class/module inheriting from Module::Info called Module::Info::File. It only holds one method called new_from_file and implements my patches, it is available now from a CPAN mirror near you.