Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: oneliner to get module version

by graff (Chancellor)
on Jun 14, 2009 at 23:54 UTC ( #771490=note: print w/replies, xml ) Need Help??

in reply to oneliner to get module version

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{'$'}" }
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.

Replies are listed 'Best First'.
Re: oneliner to get module version
by Your Mother (Bishop) on Jun 15, 2009 at 03:57 UTC

    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/
      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^2: oneliner to get module version
by sflitman (Hermit) on Jun 15, 2009 at 03:12 UTC
    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?)


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2020-10-01 02:42 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (173 votes). Check out past polls.