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

oneliner to get module version

by sflitman (Hermit)
on Jun 14, 2009 at 19:35 UTC ( #771461=snippet: print w/ replies, xml ) Need Help??

Description: Sometimes it helps to have a quickie way to get the version from a module. This lets me do it from the command line.
# PUT IN .bashrc FILE

perlver() {
   local m=$1
   perl -M$m -e "print $m->VERSION,qq{\n};"
}

# EXECUTE FROM COMMAND LINE AS perlver <module>
Comment on oneliner to get module version
Download Code
Re: oneliner to get module version
by graff (Chancellor) on Jun 14, 2009 at 23:54 UTC
    Right -- I have something quite similar (but a little shorter ;) in my .bashrc:
    pmversion () { perl -M$1 -le "print $1->VERSION" }
    Note that bash interpolates both instances of "$1" before perl gets to read the script.

    Also, since I tend to work in a rather "diversified" network environment, it's useful to be able to spot the path to a given module:

    pmpath () { perl -M$1 -le "print \$INC{'$1.pm'}" }
    Here, I have to guard (escape) the "$" in front of INC so the shell leaves it as-is and perl sees it as "$INC"; bash ignores the single-quotes because they're inside "...", and perl does the right thing there, because $1 has already been interpolated by the shell.

    UPDATE: The "pmpath" approach above only works for "top-level" modules. In order to get the path for a module like "Foo::Bar::Baz", it's a bit more complicated:

    pmpath () { perl -M$1 -le "(\$_='$1')=~s{::}{/}g; print \$INC{\$_.'.pm'}" }
    My apologies for the misleading initial version.

    FINAL UPDATE: There's actually a much better method for finding the path to a module, as pointed out by Your Mother below.

      That is shorter, and I like pmpath().

      I have always thought I needed to use local for a Bash function since the args might change from line to line, but you're right, that's really for a multiline shell function (or am I just being paranoid?)

      SSF

      Does your pmpath function get you something perldoc -l doesn't?

      cow@moo[609]~/bin>perldoc -l CGI /usr/local/lib/perl5/5.10.0/CGI.pm
        Does your pmpath function get you something perldoc -l doesn't?

        Evidently, perldoc -l does exactly what I intended to do with my "pmpath" shell function, so now I'll just reduce it to a shell alias:

        alias pmpath='perldoc -l'
        Much better -- thank you!
Re: oneliner to get module version
by syphilis (Canon) on Jun 15, 2009 at 03:31 UTC
    Nice idea - and I've just stuck it in my bashrc on my linux box, where it's working fine.

    However, in the form in which you've posted it, it won't work with Parse::RecDescent, Inline::C and (probably) some of the other Inline::* modules. So instead, I've written the perl command as:
    perl -le "require $m;print $m->VERSION"
    and that seems to work fine for *all* modules (the ones I tested, at least).

    Cheers,
    Rob
Re: oneliner to get module version
by Anonymous Monk on Jun 15, 2009 at 07:28 UTC
Re: oneliner to get module version
by Khen1950fx (Canon) on Jun 26, 2009 at 07:25 UTC
    I guess that I'm set in my ways:-). For example, if I want to get the version number of CGI, I find myself using

    cpan -D CGI

    Old habits are hard to break.

Re: oneliner to get module version
by planetscape (Canon) on Jul 06, 2009 at 07:50 UTC
      The batch file (equivalent to bart's) that I'm using as a result of this thread is:
      @echo off perl -le "require %1; print %1->VERSION"
      As mentioned above, loading the module with '-M' or 'use' won't work with some modules - eg Inline::C, Inline::CPP, Parse::RecDescent. With bart's version.bat I get:
      C:\_32>version Parse::RecDescent Usage: perl -MLocalTest - <grammarfile> <classname> C:\_32>version Inline::C It is invalid to use 'Inline::C' directly. Please consult the Inline documentation for more information. at -e line 0 BEGIN failed--compilation aborted. C:\_32>version Inline::CPP It is invalid to use 'Inline::CPP' directly. Please consult the Inline documentation for more information. at -e line 0 BEGIN failed--compilation aborted.
      Cheers,
      Rob
Re: oneliner to get module version
by Tux (Monsignor) on Jul 07, 2009 at 07:56 UTC

    Why does nobody mention Abe's V module?

    $ perl -MV=V V /pro/lib/perl5/site_perl/5.8.8/V.pm: 0.13 $ perl -MV=DBI DBI /pro/lib/perl5/site_perl/5.8.8/x86_64-linux/DBI.pm: 1.609 $ perl -MV=charnames charnames /pro/lib/perl5/5.8.8/charnames.pm: 1.05 $

    Enjoy, Have FUN! H.Merijn
      Because they all use Devel::Modlist ? :)
      perl -MDevel::Modlist -MCGI -MSafe -Mautodie -e 1 ActivePerl::Config ActiveState::Path 1.00 AutoLoader 5.67 CGI 3.43 CGI::Util 1.5_01 Carp 1.10 Carp::Heavy Config Config_heavy.pl Cwd 3.3 C:::Perl::site::lib::sitecustomize.pl Exporter 5.63 Exporter::Heavy 5.63 Fatal 2.05 Fcntl 1.06 File::Basename 2.77 List::Util 1.21 Opcode 1.0601 POSIX 1.1501 Safe 2.16 Scalar::Util 1.21 Tie::Hash 1.03 Tie::RefHash 1.38 XSLoader 0.10 autodie 2.05 constant 1.17 overload 1.06 re 0.0601 subs 1.00 vars 1.01 warnings 1.05_01 warnings::register 1.01

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://771461]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-09-22 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls