📄 perlrun.1
字号:
.IX Xref "-I @INC".IX Item "-Idirectory"Directories specified by \fB\-I\fR are prepended to the search path formodules (\f(CW@INC\fR), and also tells the C preprocessor where to search forinclude files. The C preprocessor is invoked with \fB\-P\fR; by default itsearches /usr/include and /usr/lib/perl..IP "\fB\-l\fR[\fIoctnum\fR]" 5.IX Xref "-l $ $\e".IX Item "-l[octnum]"enables automatic line-ending processing. It has two separateeffects. First, it automatically chomps \f(CW$/\fR (the input recordseparator) when used with \fB\-n\fR or \fB\-p\fR. Second, it assigns \f(CW\*(C`$\e\*(C'\fR(the output record separator) to have the value of \fIoctnum\fR sothat any print statements will have that separator added back on.If \fIoctnum\fR is omitted, sets \f(CW\*(C`$\e\*(C'\fR to the current value of\&\f(CW$/\fR. For instance, to trim lines to 80 columns:.Sp.Vb 1\& perl \-lpe \*(Aqsubstr($_, 80) = ""\*(Aq.Ve.SpNote that the assignment \f(CW\*(C`$\e = $/\*(C'\fR is done when the switch is processed,so the input record separator can be different than the output recordseparator if the \fB\-l\fR switch is followed by a \fB\-0\fR switch:.Sp.Vb 1\& gnufind / \-print0 | perl \-ln0e \*(Aqprint "found $_" if \-p\*(Aq.Ve.SpThis sets \f(CW\*(C`$\e\*(C'\fR to newline and then sets \f(CW$/\fR to the null character..IP "\fB\-m\fR[\fB\-\fR]\fImodule\fR" 5.IX Xref "-m -M".IX Item "-m[-]module".PD 0.IP "\fB\-M\fR[\fB\-\fR]\fImodule\fR" 5.IX Item "-M[-]module".IP "\fB\-M\fR[\fB\-\fR]\fI'module ...'\fR" 5.IX Item "-M[-]'module ...'".IP "\fB\-[mM]\fR[\fB\-\fR]\fImodule=arg[,arg]...\fR" 5.IX Item "-[mM][-]module=arg[,arg]...".PD\&\fB\-m\fR\fImodule\fR executes \f(CW\*(C`use\*(C'\fR \fImodule\fR \f(CW\*(C`();\*(C'\fR before executing yourprogram..Sp\&\fB\-M\fR\fImodule\fR executes \f(CW\*(C`use\*(C'\fR \fImodule\fR \f(CW\*(C`;\*(C'\fR before executing yourprogram. You can use quotes to add extra code after the module name,e.g., \f(CW\*(Aq\-Mmodule qw(foo bar)\*(Aq\fR..SpIf the first character after the \fB\-M\fR or \fB\-m\fR is a dash (\f(CW\*(C`\-\*(C'\fR)then the 'use' is replaced with 'no'..SpA little builtin syntactic sugar means you can also say\&\fB\-mmodule=foo,bar\fR or \fB\-Mmodule=foo,bar\fR as a shortcut for\&\f(CW\*(Aq\-Mmodule qw(foo bar)\*(Aq\fR. This avoids the need to use quotes whenimporting symbols. The actual code generated by \fB\-Mmodule=foo,bar\fR is\&\f(CW\*(C`use module split(/,/,q{foo,bar})\*(C'\fR. Note that the \f(CW\*(C`=\*(C'\fR formremoves the distinction between \fB\-m\fR and \fB\-M\fR..SpA consequence of this is that \fB\-MFoo=number\fR never does a version check(unless \f(CW\*(C`Foo::import()\*(C'\fR itself is set up to do a version check, whichcould happen for example if Foo inherits from Exporter.).IP "\fB\-n\fR" 5.IX Xref "-n".IX Item "-n"causes Perl to assume the following loop around your program, whichmakes it iterate over filename arguments somewhat like \fBsed \-n\fR or\&\fBawk\fR:.Sp.Vb 4\& LINE:\& while (<>) {\& ... # your program goes here\& }.Ve.SpNote that the lines are not printed by default. See \fB\-p\fR to havelines printed. If a file named by an argument cannot be opened forsome reason, Perl warns you about it and moves on to the next file..SpHere is an efficient way to delete all files that haven't been modified forat least a week:.Sp.Vb 1\& find . \-mtime +7 \-print | perl \-nle unlink.Ve.SpThis is faster than using the \fB\-exec\fR switch of \fBfind\fR because you don'thave to start a process on every filename found. It does suffer fromthe bug of mishandling newlines in pathnames, which you can fix ifyou follow the example under \fB\-0\fR..Sp\&\f(CW\*(C`BEGIN\*(C'\fR and \f(CW\*(C`END\*(C'\fR blocks may be used to capture control before or afterthe implicit program loop, just as in \fBawk\fR..IP "\fB\-p\fR" 5.IX Xref "-p".IX Item "-p"causes Perl to assume the following loop around your program, whichmakes it iterate over filename arguments somewhat like \fBsed\fR:.Sp.Vb 6\& LINE:\& while (<>) {\& ... # your program goes here\& } continue {\& print or die "\-p destination: $!\en";\& }.Ve.SpIf a file named by an argument cannot be opened for some reason, Perlwarns you about it, and moves on to the next file. Note that thelines are printed automatically. An error occurring during printing istreated as fatal. To suppress printing use the \fB\-n\fR switch. A \fB\-p\fRoverrides a \fB\-n\fR switch..Sp\&\f(CW\*(C`BEGIN\*(C'\fR and \f(CW\*(C`END\*(C'\fR blocks may be used to capture control before or afterthe implicit loop, just as in \fBawk\fR..IP "\fB\-P\fR" 5.IX Xref "-P".IX Item "-P"\&\fB\s-1NOTE:\s0 Use of \-P is strongly discouraged because of its inherentproblems, including poor portability. It is deprecated and will beremoved in a future version of Perl.\fR.SpThis option causes your program to be run through the C preprocessor beforecompilation by Perl. Because both comments and \fBcpp\fR directives beginwith the # character, you should avoid starting comments with any wordsrecognized by the C preprocessor such as \f(CW"if"\fR, \f(CW"else"\fR, or \f(CW"define"\fR..SpIf you're considering using \f(CW\*(C`\-P\*(C'\fR, you might also want to look at theFilter::cpp module from \s-1CPAN\s0..SpThe problems of \-P include, but are not limited to:.RS 5.IP "\(bu" 10The \f(CW\*(C`#!\*(C'\fR line is stripped, so any switches there don't apply..IP "\(bu" 10A \f(CW\*(C`\-P\*(C'\fR on a \f(CW\*(C`#!\*(C'\fR line doesn't work..IP "\(bu" 10\&\fBAll\fR lines that begin with (whitespace and) a \f(CW\*(C`#\*(C'\fR butdo not look like cpp commands, are stripped, including anythinginside Perl strings, regular expressions, and here-docs ..IP "\(bu" 10In some platforms the C preprocessor knows too much: it knows aboutthe \*(C+ \-style until-end-of-line comments starting with \f(CW"//"\fR.This will cause problems with common Perl constructs like.Sp.Vb 1\& s/foo//;.Ve.Spbecause after \-P this will became illegal code.Sp.Vb 1\& s/foo.Ve.SpThe workaround is to use some other quoting separator than \f(CW"/"\fR,like for example \f(CW"!"\fR:.Sp.Vb 1\& s!foo!!;.Ve.IP "\(bu" 10It requires not only a working C preprocessor but also a working\&\fIsed\fR. If not on \s-1UNIX\s0, you are probably out of luck on this..IP "\(bu" 10Script line numbers are not preserved..IP "\(bu" 10The \f(CW\*(C`\-x\*(C'\fR does not work with \f(CW\*(C`\-P\*(C'\fR..RE.RS 5.RE.IP "\fB\-s\fR" 5.IX Xref "-s".IX Item "-s"enables rudimentary switch parsing for switches on the commandline after the program name but before any filename arguments (or beforean argument of \fB\-\-\fR). Any switch found there is removed from \f(CW@ARGV\fR and sets thecorresponding variable in the Perl program. The following programprints \*(L"1\*(R" if the program is invoked with a \fB\-xyz\fR switch, and \*(L"abc\*(R"if it is invoked with \fB\-xyz=abc\fR..Sp.Vb 2\& #!/usr/bin/perl \-s\& if ($xyz) { print "$xyz\en" }.Ve.SpDo note that a switch like \fB\-\-help\fR creates the variable ${\-help}, which is not compliantwith \f(CW\*(C`strict refs\*(C'\fR. Also, when using this option on a script withwarnings enabled you may get a lot of spurious \*(L"used only once\*(R" warnings..IP "\fB\-S\fR" 5.IX Xref "-S".IX Item "-S"makes Perl use the \s-1PATH\s0 environment variable to search for theprogram (unless the name of the program contains directory separators)..SpOn some platforms, this also makes Perl append suffixes to thefilename while searching for it. For example, on Win32 platforms,the \*(L".bat\*(R" and \*(L".cmd\*(R" suffixes are appended if a lookup for theoriginal name fails, and if the name does not already end in oneof those suffixes. If your Perl was compiled with \s-1DEBUGGING\s0 turnedon, using the \-Dp switch to Perl shows how the search progresses..SpTypically this is used to emulate #! startup on platforms that don'tsupport #!. Its also convenient when debugging a script that uses #!,and is thus normally found by the shell's \f(CW$PATH\fR search mechanism..SpThis example works on many platforms that have a shell compatible withBourne shell:.Sp.Vb 3\& #!/usr/bin/perl\& eval \*(Aqexec /usr/bin/perl \-wS $0 ${1+"$@"}\*(Aq\& if $running_under_some_shell;.Ve.SpThe system ignores the first line and feeds the program to \fI/bin/sh\fR,which proceeds to try to execute the Perl program as a shell script.The shell executes the second line as a normal shell command, and thusstarts up the Perl interpreter. On some systems \f(CW$0\fR doesn't alwayscontain the full pathname, so the \fB\-S\fR tells Perl to search for theprogram if necessary. After Perl locates the program, it parses thelines and ignores them because the variable \f(CW$running_under_some_shell\fRis never true. If the program will be interpreted by csh, you will needto replace \f(CW\*(C`${1+"$@"}\*(C'\fR with \f(CW$*\fR, even though that doesn't understandembedded spaces (and such) in the argument list. To start up sh ratherthan csh, some systems may have to replace the #! line with a linecontaining just a colon, which will be politely ignored by Perl. Othersystems can't control that, and need a totally devious construct thatwill work under any of \fBcsh\fR, \fBsh\fR, or Perl, such as the following:.Sp.Vb 3\& eval \*(Aq(exit $?0)\*(Aq && eval \*(Aqexec perl \-wS $0 ${1+"$@"}\*(Aq\& & eval \*(Aqexec /usr/bin/perl \-wS $0 $argv:q\*(Aq\& if $running_under_some_shell;.Ve.SpIf the filename supplied contains directory separators (i.e., is anabsolute or relative pathname), and if that file is not found,platforms that append file extensions will do so and try to lookfor the file with those extensions added, one by one..SpOn DOS-like platforms, if the program does not contain directoryseparators, it will first be searched for in the current directorybefore being searched for on the \s-1PATH\s0. On Unix platforms, theprogram will be searched for strictly on the \s-1PATH\s0..IP "\fB\-t\fR" 5.IX Xref "-t".IX Item "-t"Like \fB\-T\fR, but taint checks will issue warnings rather than fatalerrors. These warnings can be controlled normally with \f(CW\*(C`no warningsqw(taint)\*(C'\fR..Sp\&\fB\s-1NOTE:\s0 this is not a substitute for \-T.\fR This is meant only to beused as a temporary development aid while securing legacy code:for real production code and for new secure code written from scratchalways use the real \fB\-T\fR..IP "\fB\-T\fR" 5.IX Xref "-T".IX Item "-T"forces \*(L"taint\*(R" checks to be turned on so you can test them. Ordinarilythese checks are done only when running setuid or setgid. It's agood idea to turn them on explicitly for programs that run on behalfof someone else whom you might not necessarily trust, such as \s-1CGI\s0programs or any internet servers you might write in Perl. Seeperlsec for details. For security reasons, this option must beseen by Perl quite early; usually this means it must appear earlyon the command line or in the #! line for systems which supportthat construct..IP "\fB\-u\fR" 5.IX Xref "-u".IX Item "-u"This obsolete switch causes Perl to dump core after compiling yourprogram. You can then in theory take this core dump and turn itinto an executable file by using the \fBundump\fR program (not supplied).This speeds startup at the expense of some disk space (which youcan minimize by stripping the executable). (Still, a \*(L"hello world\*(R"executable comes out to about 200K on my machine.) If you want toexecute a portion of your program before dumping, use the \fIdump()\fRoperator instead. Note: availability of \fBundump\fR is platformspecific and may not be available for a specific port of Perl..IP "\fB\-U\fR" 5.IX Xref "-U".IX Item "-U"allows Perl to do unsafe operations. Currently the only \*(L"unsafe\*(R"operations are attempting to unlink directories while running as superuser, and running setuid programs with fatal taint checks turnedinto warnings. Note that the \fB\-w\fR switch (or the \f(CW$^W\fR variable) must be used along with this option to actually \fIgenerate\fR thetaint-check warnings..IP "\fB\-v\fR" 5.IX Xref "-v".IX Item "-v"prints the version and patchlevel of your perl executable..IP "\fB\-V\fR" 5.IX Xref "-V".IX Item "-V"prints summary of the major perl configuration values and the currentvalues of \f(CW@INC\fR..IP "\fB\-V:\fR\fIconfigvar\fR" 5.IX Item "-V:configvar"Prints to \s-1STDOUT\s0 the value of the named configuration variable(s),with multiples when your configvar argument looks like a regex (hasnon-letters). For example:.Sp.Vb 12\& $ perl \-V:libc\& libc=\*(Aq/lib/libc\-2.2.4.so\*(Aq;\& $ perl \-V:lib.\& libs=\*(Aq\-lnsl \-lgdbm \-ldb \-ldl \-lm \-lcrypt \-lutil \-lc\*(Aq;\& libc=\*(Aq/lib/libc\-2.2.4.so\*(Aq;\& $ perl \-V:lib.*\& libpth=\*(Aq/usr/local/lib /lib /usr/lib\*(Aq;\& libs=\*(Aq\-lnsl \-lgdbm \-ldb \-ldl \-lm \-lcrypt \-lutil \-lc\*(Aq;\& lib_ext=\*(Aq.a\*(Aq;\& libc=\*(Aq/lib/libc\-2.2.4.so\*(Aq;\& libperl=\*(Aqlibperl.a\*(Aq;\& .....Ve.SpAdditionally, extra colons can be used to control formatting. Atrailing colon suppresses the linefeed and terminator ';', allowingyou to embed queries into shell commands. (mnemonic: \s-1PATH\s0 separator\&':'.).Sp.Vb 2\& $ echo "compression\-vars: " \`perl \-V:z.*: \` " are here !"\& compression\-vars: zcat=\*(Aq\*(Aq zip=\*(Aqzip\*(Aq are here !.Ve.SpA leading colon removes the 'name=' part of the response, this allowsyou to map to the name you need. (mnemonic: empty label).Sp.Vb 2\& $ echo "goodvfork="\`./perl \-Ilib \-V::usevfork\`\& goodvfork=false;.Ve.SpLeading and trailing colons can be used together if you needpositional parameter values without the names. Note that in the casebelow, the \s-1PERL_API\s0 params are returned in alphabetical order..Sp.Vb 2\& $ echo building_on \`perl \-V::osname: \-V::PERL_API_.*:\` now\& building_on \*(Aqlinux\*(Aq \*(Aq5\*(Aq \*(Aq1\*(Aq \*(Aq9\*(Aq now.Ve.IP "\fB\-w\fR" 5.IX Xref "-w".IX Item "-w"prints warnings about dubious constructs, such as variable namesthat are mentioned only once and scalar variables that are usedbefore being set, redefined subroutines, references to undefinedfilehandles or filehandles opened read-only that you are attemptingto write on, values used as a number that don't look like numbers,using an array as though it were a scalar, if your subroutinesrecurse more than 100 deep, and innumerable other things..SpThis switch really just enables the internal \f(CW$^W\fR variable. Youcan disable or promote into fatal errors specific warnings using\&\f(CW\*(C`_\|_WARN_\|_\*(C'\fR hooks, as described in perlvar and \*(L"warn\*(R" in perlfunc.See also perldiag and perltrap. A new, fine-grained warningfacility is also available if you want to manipulate entire classesof warnings; see warnings or perllexwarn..IP "\fB\-W\fR" 5.IX Xref "-W".IX Item "-W"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -