你好,perldoc,生产力提升工具

想象一下这个场景:你正在使用DateTime模块,但你记不起它提供的具体函数名。你该怎么办?你可以打开浏览器,访问MetaCPAN,搜索DateTime并查找模块文档中的答案。更快的方法是切换到命令行,并输入perldoc DateTime以在终端中直接显示模块的文档。

perldoc是一个用于读取Perl文档的命令行程序。它与Perl一起提供,所以如果你已经安装了Perl,那么也应该有perldoc。使用perldoc很简单:正如你所看到的,要查看已安装模块的文档,请在命令行中输入

$ perldoc Module::Name

perldoc会搜索模块,如果找到它,就会显示该模块的文档(用Pod编写的)。使用perldoc你可以几乎立即了解Perl的任何方面。想了解更多关于Pod的信息?尝试perldoc pod

按功能键查找

Perl有大量内置函数,根据Perl版本的不同,大约有224个。谁能记得它们的所有用法?我知道我不能。要查找Perl函数,请使用带有-f选项的perldoc。例如,要查找rindex函数的文档

$ perldoc -f rindex

这将显示

rindex STR,SUBSTR,POSITION
rindex STR,SUBSTR
      Works just like index() except that it returns the position of the
      last occurrence of SUBSTR in STR. If POSITION is specified,
      returns the last occurrence beginning at or before that position.

啊哈!这一切都很好,但如果你忘记了函数名怎么办?你可以使用B::Keywords,但另一种方法是查看perlfunc,这是关于内置函数的Perl文档。要阅读它,只需在perldoc中输入

$ perldoc perlfunc

预定义变量

与函数一样,Perl有大量预定义变量,它们的功能从存储程序名称到跟踪正则表达式引擎的状态。它们非常有用,但通常具有像$^O(操作系统名称)这样的晦涩难懂的名字。所以如果你发现自己需要检查是否正在查看列表分隔符($")或输出分隔符($/),只需使用带有-v选项的perldoc即可

$ perldoc -v $/

由于一些预定义变量具有奇特的名字,你可能需要在命令行中引用它们才能使perldoc工作

$ perldoc -v '$"'

预定义变量文档被称为perlvar。至少值得阅读一次(perldoc perlvar)。

搜索文档

Perl有很多优秀的文档,但很难记住所有条目的名称。如果你想要浏览目录,请使用perl

$ perldoc perl

许多人推荐使用perltoc进行此操作,但我认为对于查找相关条目,使用perl条目比perltoc更容易浏览。

Perl还有一个详尽的FAQ(另一个值得阅读的条目)。它包含了大量常见问题的答案。像往常一样,你可以使用perldoc faq来阅读它,但你也可以使用-q选项来搜索它。想知道是否有任何适合Perl的好IDE吗?(一个常见的新手问题)

$ perl -q ide
Is there an IDE or Windows Perl Editor?
 Perl programs are just plain text, so any editor will do.

 If you're on Unix, you already have an IDE--Unix itself. The Unix
 philosophy is the philosophy of several small tools that each do one thing
 and do it well. It's like a carpenter's toolbox.

 If you want an IDE, check the following (in alphabetical order, not order
 of preference):

 Eclipse


     The Eclipse Perl Integration Project integrates Perl editing/debugging
     with Eclipse.

 Komodo


     ActiveState's cross-platform (as of October 2004, that's Windows,
     Linux, and Solaris), multi-language IDE has Perl support, including a
     regular expression debugger and remote debugging.
...

查找模块安装位置

perldoc不仅仅是关于文档。如果你需要找出模块的安装位置,使用-l选项,perldoc将返回模块的文件路径

$ perldoc -l Test::More

如果你得到了路径,你可以直接在编辑器中打开它

$ vi $(perldoc -l Test::More)

一个小技巧:一些模块中没有Pod,对于这些模块,使用-lm选项仍然可以返回路径。

在perldoc中读取模块源代码

最后,perldoc还可以显示模块源代码。只需使用-m选项即可

$ perldoc -m Test::More

总结

本文已经介绍了最常见的功能,但perldoc还有许多其他功能,您可以在命令行通过man perldoc来阅读。Perl文档也在线提供

记住,随着您对perldoc的熟练程度提高,您对在线资源的依赖会越来越少。养成在命令行切换、在perldoc中查找信息,然后立即回到编程的习惯——这将提高您的效率。

perldoc速查表

perldoc [option]

Module Options                               
--------------                               
         Module documentation     
-l       Module filepath          
-lm      Module filepath (alt.)   
-m       Module source


Search Options
--------------
-f     Get a built-in function definition
-v     Get a variable definition
-q      Search the faq for a keyword


Commonly Used Entries
---------------------
perl         Language overview, list of all other entries
perltoc      Table of contents
perlfunc     Built-in functions documentation
perlvar      Predefined variables documentation
perlref      References documentation
perlre       Regex documentation
faq          The Perl FAQ

Help
----
man perldoc     List of all perldoc options


本文最初发布在PerlTricks.com

标签

David Farrell

David是一位职业程序员,他经常在推特博客上分享关于代码和编程艺术的见解。

浏览他们的文章

反馈

这篇文章有什么问题吗?请通过在GitHub上打开问题或拉取请求来帮助我们。