Perl命令行:一行式程序
语法perl命令行在运行"一行式程序"非常有用,用法如下: [[email?protected] ~]# perl --help Usage: perl [switches] [--] [programfile] [arguments] -0[octal] specify record separator ( ,if no argument) -a autosplit mode with -n or -p (splits $_ into @F) -C[number/list] enables the listed Unicode features -c check syntax only (runs BEGIN and CHECK blocks) -d[:debugger] run program under debugger -D[number/list] set debugging flags (argument is a bit mask or alphabets) -e program one line of program (several -e's allowed,omit programfile) -E program like -e,but enables all optional features -f don't do $sitelib/sitecustomize.pl at startup -F/pattern/ split() pattern for -a switch (//'s are optional) -i[extension] edit <> files in place (makes backup if extension supplied) -Idirectory specify @INC/#include directory (several -I's allowed) -l[octal] enable line ending processing,specifies line terminator -[mM][-]module execute "use/no module..." before executing program -n assume "while (<>) { ... }" loop around program -p assume loop like -n but print line also,like sed -s enable rudimentary parsing for switches after programfile -S 使用PATH环境变量搜索程序文件 -t enable tainting warnings -T enable tainting checks -u 在解析程序之后dump core -U 允许不安全的操作(其中之一:root身份删除目录) -v 输出版本号、补丁版本 -V 输出Perl的主要配置概要和@INC、@ENV的当前值 -V:variable 输出Config.pm中的某个变量 -w 启用大多数warnings信息 -W 启用所有的warning信息 -x[directory] 忽略#!perl行前面的所有文本(如果指定directory,则先切换到该目录) -X 禁止所有的warnings信息 单行式程序选项选项e
perl -e 'print "haha!!!n"' 它就相当于在运行一个包含了如下代码的perl程序: print "haha!!!n" -e是使用perl单行程序的开关,使用该选项后,perl将不再从参数中读取文件名。 可以同时指定多个"-e"。 选项n和p
LINE: while(<>){ ...-e expression CODE HERE... } 由于while中使用的是
LINE: while(<>){ ...-e expression CODE HERE... }continue{ print or die "-p destination: $!n"; }
选项l
-l -lOCTNUM 该选项有两个效果:
需要注意的是,省略OCTNUM的时候,也就是只有 # 处理-l的时候 $ = $/; # 处理-0的时候 $/ = ; 上面的选项不能将 选项i
如果没有给定SUFFIX,则直接覆盖的源文件中保存,如果指定了后缀SUFFIX,则将源文件以后缀名SUFFIX的方式备份起来,然后将处理结果保存到源文件名中。 如果SUFFIX中不包含星号 在内部,perl先重命名文件,然后用原文件名打开输出文件,并把该输出文件作为print、printf和write的默认文件句柄的对象。 选项a和F
通过 例如: perl -ane 'print pop(@F),"n";' 等价于: LINE: while(<>){ @F = split(' '); print pop(@F),"n"; } 可以使用 选项0
|