Remote Perldoc

I've written a wrapper function for perldoc that will display perldocs for modules you don't have installed, with case insensitivity, or go to the http://search.cpan.org search page if the module can't be found in the CPAN index.

perldoc() {
  if [ $# = 1 ]; then
    if perldoc -l $1 >/dev/null 2>&1; then
      command perldoc $1
    else
      zgrep -i '^'$1'\>' ~/.cpan/sources/modules/02packages.details.txt | \
	head -1 | awk '{print $1}' | \
      (
	  read name
        if [ -n "$name" ]; then
          perl -MWWW::Mechanize -le 'my $mech = WWW::Mechanize->new; \
            $mech->get("http://search.cpan.org/perldoc?'$name'"); \
            $mech->follow_link(text_regex => qr/^source\z/i); \
            $mech->save_content("/tmp/${$}_perldoc.pod"); \
            print "/tmp/${$}_perldoc.pod"' | \
  	    (
            read pod
            perldoc $pod
            rm $pod
  	    )
        else
          elinks "http://search.cpan.org/search?query=${1}&mode=all"
        fi
      )
    fi
  else
    command perldoc "$@"
  fi
}


This works in bash and zsh. Just put it in your ~/.bashrc or ~/.zshrc.

Zsh users: I also recommend turning off the annoying and incredibly slow module completion feature, which gets in the way when you're trying to run perldoc on a file path.

compdef _files perldoc

Last modified: 2017-1-8 (日) at 9:14 pm