📄 perlrun.pod
字号:
and the C<open> pragma (see L<open>).(In Perls earlier than 5.8.1 the C<-C> switch was a Win32-only switchthat enabled the use of Unicode-aware "wide system call" Win32 APIs.This feature was practically unused, however, and the command lineswitch was therefore "recycled".)=item B<-c>X<-c>causes Perl to check the syntax of the program and then exit withoutexecuting it. Actually, it I<will> execute C<BEGIN>, C<UNITCHECK>,C<CHECK>, and C<use> blocks, because these are considered as occurringoutside the execution of your program. C<INIT> and C<END> blocks,however, will be skipped.=item B<-d>X<-d> X<-dt>=item B<-dt>runs the program under the Perl debugger. See L<perldebug>.If B<t> is specified, it indicates to the debugger that threadswill be used in the code being debugged.=item B<-d:>I<foo[=bar,baz]>X<-d> X<-dt>=item B<-dt:>I<foo[=bar,baz]>runs the program under the control of a debugging, profiling, ortracing module installed as Devel::foo. E.g., B<-d:DProf> executesthe program using the Devel::DProf profiler. As with the B<-M>flag, options may be passed to the Devel::foo package where theywill be received and interpreted by the Devel::foo::import routine.The comma-separated list of options must follow a C<=> character.If B<t> is specified, it indicates to the debugger that threadswill be used in the code being debugged.See L<perldebug>.=item B<-D>I<letters>X<-D> X<DEBUGGING> X<-DDEBUGGING>=item B<-D>I<number>sets debugging flags. To watch how it executes your program, useB<-Dtls>. (This works only if debugging is compiled into yourPerl.) Another nice value is B<-Dx>, which lists your compiledsyntax tree. And B<-Dr> displays compiled regular expressions;the format of the output is explained in L<perldebguts>.As an alternative, specify a number instead of list of letters (e.g.,B<-D14> is equivalent to B<-Dtls>): 1 p Tokenizing and parsing (with v, displays parse stack) 2 s Stack snapshots (with v, displays all stacks) 4 l Context (loop) stack processing 8 t Trace execution 16 o Method and overloading resolution 32 c String/numeric conversions 64 P Print profiling info, preprocessor command for -P, source file input state 128 m Memory allocation 256 f Format processing 512 r Regular expression parsing and execution 1024 x Syntax tree dump 2048 u Tainting checks 4096 U Unofficial, User hacking (reserved for private, unreleased use) 8192 H Hash dump -- usurps values() 16384 X Scratchpad allocation 32768 D Cleaning up 65536 S Thread synchronization 131072 T Tokenising 262144 R Include reference counts of dumped variables (eg when using -Ds) 524288 J Do not s,t,P-debug (Jump over) opcodes within package DB 1048576 v Verbose: use in conjunction with other flags 2097152 C Copy On Write 4194304 A Consistency checks on internal structures 8388608 q quiet - currently only suppresses the "EXECUTING" messageAll these flags require B<-DDEBUGGING> when you compile the Perlexecutable (but see L<Devel::Peek>, L<re> which may change this).See the F<INSTALL> file in the Perl source distributionfor how to do this. This flag is automatically set if you include B<-g>option when C<Configure> asks you about optimizer/debugger flags.If you're just trying to get a print out of each line of Perl codeas it executes, the way that C<sh -x> provides for shell scripts,you can't use Perl's B<-D> switch. Instead do this # If you have "env" utility env PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS program # Bourne shell syntax $ PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS program # csh syntax % (setenv PERLDB_OPTS "NonStop=1 AutoTrace=1 frame=2"; perl -dS program)See L<perldebug> for details and variations.=item B<-e> I<commandline>X<-e>may be used to enter one line of program. If B<-e> is given, Perlwill not look for a filename in the argument list. Multiple B<-e>commands may be given to build up a multi-line script. Make sureto use semicolons where you would in a normal program.=item B<-E> I<commandline>X<-E>behaves just like B<-e>, except that it implicitly enables alloptional features (in the main compilation unit). See L<feature>.=item B<-f>X<-f>Disable executing F<$Config{sitelib}/sitecustomize.pl> at startup.Perl can be built so that it by default will try to executeF<$Config{sitelib}/sitecustomize.pl> at startup. This is a hook thatallows the sysadmin to customize how perl behaves. It can forinstance be used to add entries to the @INC array to make perl findmodules in non-standard locations.=item B<-F>I<pattern>X<-F>specifies the pattern to split on if B<-a> is also in effect. Thepattern may be surrounded by C<//>, C<"">, or C<''>, otherwise it will beput in single quotes. You can't use literal whitespace in the pattern.=item B<-h>X<-h>prints a summary of the options.=item B<-i>[I<extension>]X<-i> X<in-place>specifies that files processed by the C<E<lt>E<gt>> construct are to beedited in-place. It does this by renaming the input file, opening theoutput file by the original name, and selecting that output file as thedefault for print() statements. The extension, if supplied, is used tomodify the name of the old file to make a backup copy, following theserules:If no extension is supplied, no backup is made and the current file isoverwritten.If the extension doesn't contain a C<*>, then it is appended to theend of the current filename as a suffix. If the extension doescontain one or more C<*> characters, then each C<*> is replacedwith the current filename. In Perl terms, you could think of thisas: ($backup = $extension) =~ s/\*/$file_name/g;This allows you to add a prefix to the backup file, instead of (or inaddition to) a suffix: $ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup to 'orig_fileA'Or even to place backup copies of the original files into anotherdirectory (provided the directory already exists): $ perl -pi'old/*.orig' -e 's/bar/baz/' fileA # backup to 'old/fileA.orig'These sets of one-liners are equivalent: $ perl -pi -e 's/bar/baz/' fileA # overwrite current file $ perl -pi'*' -e 's/bar/baz/' fileA # overwrite current file $ perl -pi'.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig' $ perl -pi'*.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig'From the shell, saying $ perl -p -i.orig -e "s/foo/bar/; ... "is the same as using the program: #!/usr/bin/perl -pi.orig s/foo/bar/;which is equivalent to #!/usr/bin/perl $extension = '.orig'; LINE: while (<>) { if ($ARGV ne $oldargv) { if ($extension !~ /\*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/\*/$ARGV/g; } rename($ARGV, $backup); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/; } continue { print; # this prints to original filename } select(STDOUT);except that the B<-i> form doesn't need to compare $ARGV to $oldargv toknow when the filename has changed. It does, however, use ARGVOUT forthe selected filehandle. Note that STDOUT is restored as the defaultoutput filehandle after the loop.As shown above, Perl creates the backup file whether or not any outputis actually changed. So this is just a fancy way to copy files: $ perl -p -i'/some/file/path/*' -e 1 file1 file2 file3...or $ perl -p -i'.orig' -e 1 file1 file2 file3...You can use C<eof> without parentheses to locate the end of each inputfile, in case you want to append to each file, or reset line numbering(see example in L<perlfunc/eof>).If, for a given file, Perl is unable to create the backup file asspecified in the extension then it will skip that file and continue onwith the next one (if it exists).For a discussion of issues surrounding file permissions and B<-i>,see L<perlfaq5/Why does Perl let me delete read-only files? Why does -i clobber protected files? Isn't this a bug in Perl?>.You cannot use B<-i> to create directories or to strip extensions fromfiles.Perl does not expand C<~> in filenames, which is good, since somefolks use it for their backup files: $ perl -pi~ -e 's/foo/bar/' file1 file2 file3...Note that because B<-i> renames or deletes the original file beforecreating a new file of the same name, UNIX-style soft and hard links willnot be preserved.Finally, the B<-i> switch does not impede execution when nofiles are given on the command line. In this case, no backup is made(the original file cannot, of course, be determined) and processingproceeds from STDIN to STDOUT as might be expected.=item B<-I>I<directory>X<-I> X<@INC>Directories specified by B<-I> are prepended to the search path formodules (C<@INC>), and also tells the C preprocessor where to search forinclude files. The C preprocessor is invoked with B<-P>; by default itsearches /usr/include and /usr/lib/perl.=item B<-l>[I<octnum>]X<-l> X<$/> X<$\>enables automatic line-ending processing. It has two separateeffects. First, it automatically chomps C<$/> (the input recordseparator) when used with B<-n> or B<-p>. Second, it assigns C<$\>(the output record separator) to have the value of I<octnum> sothat any print statements will have that separator added back on.If I<octnum> is omitted, sets C<$\> to the current value ofC<$/>. For instance, to trim lines to 80 columns: perl -lpe 'substr($_, 80) = ""'Note that the assignment C<$\ = $/> is done when the switch is processed,so the input record separator can be different than the output recordseparator if the B<-l> switch is followed by a B<-0> switch: gnufind / -print0 | perl -ln0e 'print "found $_" if -p'This sets C<$\> to newline and then sets C<$/> to the null character.=item B<-m>[B<->]I<module>X<-m> X<-M>=item B<-M>[B<->]I<module>=item B<-M>[B<->]I<'module ...'>=item B<-[mM]>[B<->]I<module=arg[,arg]...>B<-m>I<module> executes C<use> I<module> C<();> before executing yourprogram.B<-M>I<module> executes C<use> I<module> C<;> before executing yourprogram. You can use quotes to add extra code after the module name,e.g., C<'-Mmodule qw(foo bar)'>.If the first character after the B<-M> or B<-m> is a dash (C<->)then the 'use' is replaced with 'no'.A little builtin syntactic sugar means you can also sayB<-mmodule=foo,bar> or B<-Mmodule=foo,bar> as a shortcut forC<'-Mmodule qw(foo bar)'>. This avoids the need to use quotes whenimporting symbols. The actual code generated by B<-Mmodule=foo,bar> isC<use module split(/,/,q{foo,bar})>. Note that the C<=> formremoves the distinction between B<-m> and B<-M>.A consequence of this is that B<-MFoo=number> never does a version check(unless C<Foo::import()> itself is set up to do a version check, whichcould happen for example if Foo inherits from Exporter.)=item B<-n>X<-n>causes Perl to assume the following loop around your program, whichmakes it iterate over filename arguments somewhat like B<sed -n> orB<awk>: LINE: while (<>) { ... # your program goes here }Note that the lines are not printed by default. See B<-p> 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.Here is an efficient way to delete all files that haven't been modified forat least a week: find . -mtime +7 -print | perl -nle unlinkThis is faster than using the B<-exec> switch of B<find> 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 B<-0>.C<BEGIN> and C<END> blocks may be used to capture control before or afterthe implicit program loop, just as in B<awk>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -