Tired of scrambling around @INC when you want to see a module's code? Let Perl find it!

Save this in a file in your search path (I called it vipm -- guess what editor I use). Then you can just say

vipm Apache::Registry CGI #or vipm Apache/Registry.pm CGI.pm
or whatever.

Arguments starting with - are passed through. There's not a lot of error checking; give it stupid arguments, it'll probably do stupid things.

Share and enjoy.

#! /usr/bin/perl -w # # Find a module and edit it. Arguments should be either # "Perl" style: Apache::Registry # or "path" style: Apache/Registry.pm # # Any arguments starting with - are passed to editor unchanged. # foreach (@ARGV) { next if /^-/; s|/|::|g if s/\.pm$//; eval "require $_" or die "Module $_ didn't load\n"; s|::|/|g; } exec $ENV{EDITOR} || '/bin/vi', map {/^-/? $_ : $INC{$_ . '.pm'} } @AR +GV;

Replies are listed 'Best First'.
Re: Find and edit module
by merlyn (Sage) on Sep 28, 2001 at 03:47 UTC
    That'll do nasty things if the requiring of the code executes something! Perhaps a static search of @INC is more in order.

    I think there's something in TC's pmtools that does this, if you wanna steal from somewhere.

    -- Randal L. Schwartz, Perl hacker

      I mostly use this for things I install from CPAN, which are (usually!) well-behaved. But your point is well taken. It also won't work if the module has any sort of error in it.

      I found a copy of pmtools here in perl.org and I think you're referring to the pmpath program in it; to get the same effect as this, you would do vi `pmpath What::Ever`. But pmpath also has the same issue in that it does a require of the module.

      So here's a variation that walks down your @INC statically until it finds a file fitting the description:

      #!/usr/bin/perl -w ARG: foreach (@ARGV) { next if /^-/; s|::|/|g; $_ .= '.pm' unless /\.pm$/; foreach my $d (@INC) { $_ = "$d/$_", next ARG if -f "$d/$_" and -r _; } die "Can't find a module named $_\n"; } exec $ENV{EDITOR} || '/bin/vi', @ARGV;
      Arguments starting with - are still passed through unchanged, and it still doesn't do a lot of error checking.

      Props to merlyn for pushing me to improve the code. ;-)

(dkubb) Re: (1) Find and edit module
by dkubb (Deacon) on Sep 29, 2001 at 13:07 UTC

    perldoc has an -m switch, which will bring up your default editor, displaying most module's full source code:

    perldoc -m CGI
      I believe Perldoc's -m switch runs the full source code through the pager, but not into an editor. However, localizing the Perldoc pager environment variable for a single command, like so:

      $ PERLDOC_PAGER=/bin/vi perldoc -m CGI

      works on my system (Cygwin32).
Re: Find and edit module
by Jenda (Abbot) on May 29, 2004 at 17:03 UTC

    I use this (via a Doskey macro) under Windows:

    perl -e "map{s#/|::|-#\\#g;$_.='.pm'}@ARGV;for$m(@ARGV){for(@INC){die +`start $_\\$m`.qq{\n} if -e qq{$_\\$m}}}" $*
    It assumes you have the .pm extension mapped to your favourite editor, but it should be no problem to change this for other OSes and call it via a shell script or something.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature