Download
(require 'cperl-mode)
(require 'cl)
(defun replace-all (from to str)
"Replace all instances of FROM with TO in STR, and return the
result"
(while (string-match from str)
(setq str (replace-match to t t str)))
str)
(defvar *perl-lib-path* '() "Directories in which to find perl libraries in.")
(defvar *perl-libraries* '() "Cache of all known perl libraries on the system")
(defun perl-rebuild-lib-path ()
"Compute *PERL-LIB-PATH* by invoking perl and printing the
value of @INC"
(setq *perl-lib-path*
(remove-if-not
(lambda (dir) (and (string-match "/" dir ) (file-exists-p dir)))
(car
(read-from-string
(shell-command-to-string "perl -e '$\"=\"\\\" \\\"\"; print \"(\\\"@INC\\\")\"'"))))))
(defun perl-find-all-libraries ()
"Compute the value of *PERL-LIBRARIES* by searching in
*PERL-LIB-PATH* for files ending in .pod or .pm"
(if (not *perl-lib-path*)
(perl-rebuild-lib-path))
(require 'find-lisp)
(setq *perl-libraries*
(mapcan
(lambda (dir)
(mapcar
(lambda (file)
(replace-all
(rx (seq "." (| "pod" "pm" ) string-end))
""
(replace-all "/" "::" (substring file (+ 1(length dir))))))
(find-lisp-find-files dir (rx (seq "." (| "pod" "pm" ) string-end)))))
*perl-lib-path*)))
(defun perldoc (library)
"Invoke `cperl-perldoc' on LIBRARY, but do completion using *PERL-LIBRARIES*
when run interactively"
(interactive
(list (perl-read-library "Perldoc entry: ")))
(cperl-perldoc library))
(defun perl-find-library (library)
"Find a perl library by module name"
(interactive
(list (perl-read-library "Find perl library: ")))
(find-file (perl-library-path library)))
(defalias 'perl-find-file 'perl-find-library)
(defun perl-library-path (library)
"Returns the path to the perl library LIBRARY on disk."
(let ((path (shell-command-to-string (concat "perldoc -l " library))))
(and path
(substring path 0 (- (length path) 1)))))
(defun perl-read-library (&optional prompt)
"Read the name of a perl library from the minibuffer, with
completion."
(if (not prompt)
(setq prompt "Perl library: "))
(if (not *perl-libraries*)
(progn
(message "Building completion list...")
(perl-find-all-libraries)))
(completing-read
prompt
*perl-libraries*))
(provide 'perl-find-library)