Category: Utility Scripts
Author/Contact Info (reply)
Description: Use 'vi' as your 'pager' to view module source code.

This is a bit like "perldoc -m Module" then "v" to tell your pager (if it is smart enough) to throw you into your favorite editor (but less typing, supports multiple module names, leaves the paths to the modules displayed after you exit the editor, doesn't require your pager support "v", etc.)

Requires Algorithm::Loops.

(Updated to remove mispelt "pathes". Thanks, gaal.)

#!/usr/bin/perl -w
use strict;

use Algorithm::Loops qw( Filter MapCarE );

Main( @ARGV );
exit( 0 );

sub Main
{
    my @mods= @_;
    die "Usage: $0 Module::Name [...]\n"
        if  ! @mods;
    my @files= Filter {
        s#::#/#g;
        s#'#/#g;
        $_ .= ".pm"   if  ! /\./;
    } @mods;
    my @paths= MapCarE {
        my( $mod, $file )= @_;
        if(  ! eval { require $file; 1 }  ) {
            warn "No such module ($mod); $file not found.\n";
            ();
        } else {
            $INC{$file};
        }
    } \@mods, \@files;
    exit( 1 )   if  ! @paths;
    my $vi= $ENV{VISUAL} || $ENV{EDITOR} || "vi";
    print STDERR "Running $vi...\n";
    system( $vi, @paths );
    print $_, $/   for  @paths;
}
Replies are listed 'Best First'.
Re: vimod
by VSarkiss (Monsignor) on Aug 13, 2004 at 15:25 UTC

    I posted something similar (though not as elegant or flexible) a long time ago. merlyn pointed out in that same thread that doing a require of the file may cause unexpected side effects, so I replied with another version that does a static search of @INC.

    The problem I found with requireing the module was that I couldn't edit a module that had a syntax error in it, or otherwise wouldn't load.