perl
perl - Practical Extraction and Report Language
为方便翻阅, perl手册分成以下部分:
(如果你打算从头到尾通读一次, 章节顺序的安排能减少前向翻看的次数)
有关Perl模块的更多文档可以在目录/usr/local/man/下找到.
有一些是和Perl一起安装的, 也有一些第三方的模块. 用命令man(1)指定正确的目录
和启动文件即可阅读. 这些目录和文件可以用以下命令找到:
perl -le 'use Config; print "@Config{man1dir,man3dir}"'
如果目录是 /usr/local/man/man1 和 /usr/local/man/man3,
只要把 /usr/local/man/man 加到 MANPATH 环境. 如果是其他目录, 就要
都加上.
如果上面说的不起作用, 也可以用附带的 perldoc 脚本阅读模块信息.
也可以考虑更换一个新版的man程序.
如果程序出现奇怪的错误, 又不知道如何查找帮助, 先试一试
-w
参数. 一般会准确的指出错误所在.
Perl 是一种解释型的语言, 特别适合用于扫描文本文件, 从中抽取信息,
并根据这些信息生成报告.
该语言也同样适用于很多系统维护的工作. Perl 是一种实用的(易用,高效,完整)而
非一种漂亮的(紧凑,优雅,小巧)的语言.
它结合了(作者个人看法)了C, sed, awk, sh 的
优点, 熟悉这些语言的人会觉得 Perl 很容易上手.
(研究语言历史的人还可以找到 csh, Pascal, 甚至 BASIC-PLUS 的影子.)
表达式的语法和 C 十分相似. 和大多数 Unix 工具不同, Perl 不限制数据
的大小--只要有足够的存储空间, Perl 可以把整个文件的内容放进一个字符串.
递归的深度没有限制. 关联数组使用的哈希表只有在必要的时候才增长, 避免影响
性能. Perl 使用高级模式匹配技术, 能够在大量数据中快速搜索. Perl 也可以处理
二进制文本, 也可以把dbm文件当做关联数组处理.
Perl 提供了数据流追踪机制避免了很多setupid的安全漏洞, Setuid 脚本比 C 程序要更安全.
如果觉得用sed 或 awk 或 sh 完成工作
有点慢, 但不想用 C 来写, Perl就可以派上用场. 也有多工具可以把
sed 和 awk 脚本翻译成 Perl 脚本.
更多...
Perl 第5版是一个几乎全部改写的版本, 提供了以下的改进:
-
* 易用性的增强
-
写出的代码可读性更好(包括则表达式). 前版中那些隐藏的变量可以用易记的标识符
来代替. 出错信息内容更详细, 可选的警告信息能发现大部分的初级错误. 当发生
奇怪的错误时, 试试
-w
选项!!! 当然没发现出错的时候也可以用
-w
选项.
-
* 简化的语法
-
新的 yacc 语法只有旧版的一半大小. 许多不严格的语法规则得到了规定. 保留字
的数量减少了 2/3. 尽管改动如此之大, 但几乎所有的旧版 Perl 脚本都可以不经
改动继续运行.
-
* 语义范围
-
Perl 变量的定义有了语义范围, 就象 C 里的 ``auto'' 变量. 这个特性不但使
程序更有效, 而且在编写大程序时能更好的保护细节.
-
* 任意嵌套的数据结构
-
任意的数值变量, 包括数组元素, 可以取值为对其他变量或子过程的引用.
你可以很容易建立匿名的变量和子过程. Perl 自动维护引用记数.
-
* 模块化和重用性
-
Perl 的库现在是以模块(module)的概念定义, 模块很容易在不同的包之间共享.
一个包可以选择输出模块中定义的所有或是一部分接口. 编译指引也使用了同样
的机制
-
* 面向对象编程
-
一个包可以是一个类. 动态多继承和虚方法用直接传递的方式支持, 新增的语法不多.
文件句柄可以当作对象处理.
-
* 内嵌性和扩展性
-
现在可以在 C/C++ 程序中嵌入 Perl, 通过公开的接口, 过程可以调用和被调用.
提供了 XS 预处理器来完成组合 C/C++ 和 Perl 的工作. 支持模块的动态装载.
-
* 兼容 POSIX
-
提供了 POSIX 模块, 通过该模块可以访问所有可用的 POSIX 过程和定义.
-
* Package constructors and destructors
-
The new BEGIN and END blocks provide means to capture control as
a package is being compiled, and after the program exits. As a
degenerate case they work just like awk's BEGIN and END when you
use the
-p
or
-n
switches.
-
* 并发访问多种 DBM
-
Perl 程序可以在同一个脚本里同时访问 DBM, NDBM, SDBM, GDBM, 和 Berkeley DB 文件.
事实上, 原先的 dbmopen 接口已经被改造, 允许把任何变量指定到
某个对象类上, 由对象类定义自己的访问方式.
-
* Subroutine definitions may now be autoloaded
-
In fact, the AUTOLOAD mechanism also allows you to define any arbitrary
semantics for undefined subroutine calls. It's not just for autoloading.
-
* 正则表达式增强
-
可以指定非贪婪的数量词. 组合时不必再建立向后引用. 正则表达式可以嵌入空白
和注释增加可读性. 由于增加了一致性扩展机制, 向上兼容所有的旧式正则表达式.
够了吧...
-
HOME
-
当 chdir 没有参数时使用.
-
LOGDIR
-
当 chdir 没有参数而 HOME 未设置时使用.
-
PATH
-
执行子进程时使用, 当使用了
-S
参数时用于查找脚本位置.
-
PERL5LIB
-
用冒号分隔的目录列表, 用于查找 Perl 的库文件, 这个路径优先于标准目录和当前目录.
如果 PERL5LIB 没有定义, 就使用 PERLLIB. 当运行taint checks时(脚本运行setuid或
setgid, 或使用了
-T
选项), 两个环境变量都不使用. 而用
use lib "/my/directory";
-
PERL5DB
-
用于获取调试码. 如果未设置, 使用
BEGIN { require 'perl5db.pl' }
-
PERLLIB
-
用冒号分隔的目录列表, 用于查找 Perl 的库文件, 这个路径优先于标准目录和当前目录.
如果 PERL5LIB 已经定义, 就不使用 PERLLIB.
除上所述, Perl 不使用其他环境变量, 除非是脚本本身需要, 或者子进程需要.
不过, 执行 setuid 的脚本应该在一开始包含以下的设置, 保证没有安全漏洞.
$ENV{'PATH'} = '/bin:/usr/bin'; # or whatever you need
$ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
$ENV{'IFS'} = '' if defined $ENV{'IFS'};
Larry Wall <<lwall@netlabs.com>, with the help of oodles of other folks.
"/tmp/perl-e$$" -e 命令的临时文件
"@INC" perl 5 的库文件位置
The
-w
switch produces some lovely diagnostics.
See
the perldiag manpage
for explanations of all Perl's diagnostics.
Compilation errors will tell you the line number of the error, with an
indication of the next token or token type that was to be examined.
(In the case of a script passed to Perl via
-e
switches, each
-e
is counted as one line.)
Setuid scripts have additional constraints that can produce error
messages such as ``Insecure dependency''. See
the perlsec manpage
.
Did we mention that you should definitely consider using the
-w
switch?
The
-w
switch is not mandatory.
Perl is at the mercy of your machine's definitions of various
operations such as type casting, atof() and
sprintf()
. The latter
can even trigger a coredump when passed ludicrous input values.
If your stdio requires a seek or eof between reads and writes on a
particular stream, so does Perl. (This doesn't apply to
sysread()
and
syswrite()
.)
While none of the built-in data types have any arbitrary size limits
(apart from memory size), there are still a few arbitrary limits: a
given identifier may not be longer than 255 characters, and no
component of your PATH may be longer than 255 if you use
-S
. A regular
expression may not compile to more than 32767 bytes internally.
See the perl bugs database at http://perl.com/perl/bugs/. You may
mail your bug reports (be sure to include full configuration information
as output by the myconfig program in the perl source tree) to
perlbug@perl.com.
Perl actually stands for Pathologically Eclectic Rubbish Lister, but
don't tell anyone I said that.
The Perl motto is ``There's more than one way to do it.'' Divining
how many more is left as an exercise to the reader.
The three principal virtues of a programmer are Laziness,
Impatience, and Hubris. See the Camel Book for why.