📄 perlrun.pod
字号:
=item B<-p>X<-p>causes Perl to assume the following loop around your program, whichmakes it iterate over filename arguments somewhat like B<sed>: LINE: while (<>) { ... # your program goes here } continue { print or die "-p destination: $!\n"; }If 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 B<-n> switch. A B<-p>overrides a B<-n> switch.C<BEGIN> and C<END> blocks may be used to capture control before or afterthe implicit loop, just as in B<awk>.=item B<-P>X<-P>B<NOTE: 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.>This option causes your program to be run through the C preprocessor beforecompilation by Perl. Because both comments and B<cpp> directives beginwith the # character, you should avoid starting comments with any wordsrecognized by the C preprocessor such as C<"if">, C<"else">, or C<"define">.If you're considering using C<-P>, you might also want to look at theFilter::cpp module from CPAN.The problems of -P include, but are not limited to:=over 10=item *The C<#!> line is stripped, so any switches there don't apply.=item *A C<-P> on a C<#!> line doesn't work.=item *B<All> lines that begin with (whitespace and) a C<#> butdo not look like cpp commands, are stripped, including anythinginside Perl strings, regular expressions, and here-docs .=item *In some platforms the C preprocessor knows too much: it knows aboutthe C++ -style until-end-of-line comments starting with C<"//">.This will cause problems with common Perl constructs like s/foo//;because after -P this will became illegal code s/fooThe workaround is to use some other quoting separator than C<"/">,like for example C<"!">: s!foo!!;=item *It requires not only a working C preprocessor but also a workingF<sed>. If not on UNIX, you are probably out of luck on this.=item *Script line numbers are not preserved.=item *The C<-x> does not work with C<-P>.=back=item B<-s>X<-s>enables rudimentary switch parsing for switches on the commandline after the program name but before any filename arguments (or beforean argument of B<-->). Any switch found there is removed from @ARGV and sets thecorresponding variable in the Perl program. The following programprints "1" if the program is invoked with a B<-xyz> switch, and "abc"if it is invoked with B<-xyz=abc>. #!/usr/bin/perl -s if ($xyz) { print "$xyz\n" }Do note that a switch like B<--help> creates the variable ${-help}, which is not compliantwith C<strict refs>. Also, when using this option on a script withwarnings enabled you may get a lot of spurious "used only once" warnings.=item B<-S>X<-S>makes Perl use the PATH environment variable to search for theprogram (unless the name of the program contains directory separators).On some platforms, this also makes Perl append suffixes to thefilename while searching for it. For example, on Win32 platforms,the ".bat" and ".cmd" 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 DEBUGGING turnedon, using the -Dp switch to Perl shows how the search progresses.Typically 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 $PATH search mechanism.This example works on many platforms that have a shell compatible withBourne shell: #!/usr/bin/perl eval 'exec /usr/bin/perl -wS $0 ${1+"$@"}' if $running_under_some_shell;The system ignores the first line and feeds the program to F</bin/sh>,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 $0 doesn't alwayscontain the full pathname, so the B<-S> tells Perl to search for theprogram if necessary. After Perl locates the program, it parses thelines and ignores them because the variable $running_under_some_shellis never true. If the program will be interpreted by csh, you will needto replace C<${1+"$@"}> with C<$*>, 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 B<csh>, B<sh>, or Perl, such as the following: eval '(exit $?0)' && eval 'exec perl -wS $0 ${1+"$@"}' & eval 'exec /usr/bin/perl -wS $0 $argv:q' if $running_under_some_shell;If 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.On 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 PATH. On Unix platforms, theprogram will be searched for strictly on the PATH.=item B<-t>X<-t>Like B<-T>, but taint checks will issue warnings rather than fatalerrors. These warnings can be controlled normally with C<no warningsqw(taint)>.B<NOTE: this is not a substitute for -T.> 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 B<-T>.=item B<-T>X<-T>forces "taint" 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 CGIprograms or any internet servers you might write in Perl. SeeL<perlsec> 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.=item B<-u>X<-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 B<undump> program (not supplied).This speeds startup at the expense of some disk space (which youcan minimize by stripping the executable). (Still, a "hello world"executable comes out to about 200K on my machine.) If you want toexecute a portion of your program before dumping, use the dump()operator instead. Note: availability of B<undump> is platformspecific and may not be available for a specific port of Perl.=item B<-U>X<-U>allows Perl to do unsafe operations. Currently the only "unsafe"operations are attempting to unlink directories while running as superuser, and running setuid programs with fatal taint checks turnedinto warnings. Note that the B<-w> switch (or the C<$^W> variable) must be used along with this option to actually I<generate> thetaint-check warnings. =item B<-v>X<-v>prints the version and patchlevel of your perl executable.=item B<-V>X<-V>prints summary of the major perl configuration values and the currentvalues of @INC.=item B<-V:>I<configvar>Prints to STDOUT the value of the named configuration variable(s),with multiples when your configvar argument looks like a regex (hasnon-letters). For example: $ perl -V:libc libc='/lib/libc-2.2.4.so'; $ perl -V:lib. libs='-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc'; libc='/lib/libc-2.2.4.so'; $ perl -V:lib.* libpth='/usr/local/lib /lib /usr/lib'; libs='-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc'; lib_ext='.a'; libc='/lib/libc-2.2.4.so'; libperl='libperl.a'; ....Additionally, extra colons can be used to control formatting. Atrailing colon suppresses the linefeed and terminator ';', allowingyou to embed queries into shell commands. (mnemonic: PATH separator':'.) $ echo "compression-vars: " `perl -V:z.*: ` " are here !" compression-vars: zcat='' zip='zip' are here !A leading colon removes the 'name=' part of the response, this allowsyou to map to the name you need. (mnemonic: empty label) $ echo "goodvfork="`./perl -Ilib -V::usevfork` goodvfork=false;Leading and trailing colons can be used together if you needpositional parameter values without the names. Note that in the casebelow, the PERL_API params are returned in alphabetical order. $ echo building_on `perl -V::osname: -V::PERL_API_.*:` now building_on 'linux' '5' '1' '9' now=item B<-w>X<-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.This switch really just enables the internal C<$^W> variable. Youcan disable or promote into fatal errors specific warnings usingC<__WARN__> hooks, as described in L<perlvar> and L<perlfunc/warn>.See also L<perldiag> and L<perltrap>. A new, fine-grained warningfacility is also available if you want to manipulate entire classesof warnings; see L<warnings> or L<perllexwarn>.=item B<-W>X<-W>Enables all warnings regardless of C<no warnings> or C<$^W>.See L<perllexwarn>.=item B<-X>X<-X>Disables all warnings regardless of C<use warnings> or C<$^W>.See L<perllexwarn>.=item B<-x>X<-x>=item B<-x>I<directory>tells Perl that the program is embedded in a larger chunk of unrelatedASCII text, such as in a mail message. Leading garbage will bediscarded until the first line that starts with #! and contains thestring "perl". Any meaningful switches on that line will be applied.If a directory name is specified, Perl will switch to that directorybefore running the program. The B<-x> switch controls only thedisposal of leading garbage. The program must be terminated withC<__END__> if there is trailing garbage to be ignored (the programcan process any or all of the trailing garbage via the DATA filehandleif desired).The directory, if specified, must appear immediately following the B<-x>with no intervening whitespace.=back=head1 ENVIRONMENTX<perl, environment variables>=over 12=item HOMEX<HOME>Used if chdir has no argument.=item LOGDIRX<LOGDIR>Used if chdir has no argument and HOME is not set.=item PATHX<PATH>Used in executing subprocesses, and in finding the program if B<-S> isused.=item PERL5LIBX<PERL5LIB>A list of directories in which to look for Perl libraryfiles before looking in the standard library and the currentdirectory. Any architecture-specific directories under the specified
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -