📄 perlfaq5.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLFAQ5 1".TH PERLFAQ5 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perlfaq5 \- Files and Formats ($Revision: 10126 $).SH "DESCRIPTION".IX Header "DESCRIPTION"This section deals with I/O and the \*(L"f\*(R" issues: filehandles, flushing,formats, and footers..Sh "How do I flush/unbuffer an output filehandle? Why must I do this?".IX Xref "flush buffer unbuffer autoflush".IX Subsection "How do I flush/unbuffer an output filehandle? Why must I do this?"Perl does not support truly unbuffered output (except insofar as youcan \f(CW\*(C`syswrite(OUT, $char, 1)\*(C'\fR), although it does support is \*(L"commandbuffering\*(R", in which a physical write is performed after every outputcommand..PPThe C standard I/O library (stdio) normally buffers characters sent todevices so that there isn't a system call for each byte. In most stdioimplementations, the type of output buffering and the size of thebuffer varies according to the type of device. Perl's \f(CW\*(C`print()\*(C'\fR and\&\f(CW\*(C`write()\*(C'\fR functions normally buffer output, while \f(CW\*(C`syswrite()\*(C'\fRbypasses buffering all together..PPIf you want your output to be sent immediately when you execute\&\f(CW\*(C`print()\*(C'\fR or \f(CW\*(C`write()\*(C'\fR (for instance, for some network protocols),you must set the handle's autoflush flag. This flag is the Perlvariable \f(CW$|\fR and when it is set to a true value, Perl will flush thehandle's buffer after each \f(CW\*(C`print()\*(C'\fR or \f(CW\*(C`write()\*(C'\fR. Setting \f(CW$|\fRaffects buffering only for the currently selected default filehandle.You choose this handle with the one argument \f(CW\*(C`select()\*(C'\fR call (see\&\*(L"$|\*(R" in perlvar and \*(L"select\*(R" in perlfunc)..PPUse \f(CW\*(C`select()\*(C'\fR to choose the desired handle, then set itsper-filehandle variables..PP.Vb 3\& $old_fh = select(OUTPUT_HANDLE);\& $| = 1;\& select($old_fh);.Ve.PPSome modules offer object-oriented access to handles and theirvariables, although they may be overkill if this is the only thing youdo with them. You can use \f(CW\*(C`IO::Handle\*(C'\fR:.PP.Vb 3\& use IO::Handle;\& open my( $printer ), ">", "/dev/printer"); # but is this?\& $printer\->autoflush(1);.Ve.PPor \f(CW\*(C`IO::Socket\*(C'\fR (which inherits from \f(CW\*(C`IO::Handle\*(C'\fR):.PP.Vb 2\& use IO::Socket; # this one is kinda a pipe?\& my $sock = IO::Socket::INET\->new( \*(Aqwww.example.com:80\*(Aq );\&\& $sock\->autoflush();.Ve.PPYou can also flush an \f(CW\*(C`IO::Handle\*(C'\fR object without setting\&\f(CW\*(C`autoflush\*(C'\fR. Call the \f(CW\*(C`flush\*(C'\fR method to flush the buffer yourself:.PP.Vb 3\& use IO::Handle;\& open my( $printer ), ">", "/dev/printer"); \& $printer\->flush; # one time flush.Ve.Sh "How do I change, delete, or insert a line in a file, or append to the beginning of a file?".IX Xref "file, editing".IX Subsection "How do I change, delete, or insert a line in a file, or append to the beginning of a file?"(contributed by brian d foy).PPThe basic idea of inserting, changing, or deleting a line from a textfile involves reading and printing the file to the point you want tomake the change, making the change, then reading and printing the restof the file. Perl doesn't provide random access to lines (especiallysince the record input separator, \f(CW$/\fR, is mutable), although modulessuch as \f(CW\*(C`Tie::File\*(C'\fR can fake it..PPA Perl program to do these tasks takes the basic form of opening afile, printing its lines, then closing the file:.PP.Vb 2\& open my $in, \*(Aq<\*(Aq, $file or die "Can\*(Aqt read old file: $!";\& open my $out, \*(Aq>\*(Aq, "$file.new" or die "Can\*(Aqt write new file: $!";\&\& while( <$in> )\& {\& print $out $_;\& }\&\& close $out;.Ve.PPWithin that basic form, add the parts that you need to insert, change,or delete lines..PPTo prepend lines to the beginning, print those lines before you enterthe loop that prints the existing lines..PP.Vb 2\& open my $in, \*(Aq<\*(Aq, $file or die "Can\*(Aqt read old file: $!";\& open my $out, \*(Aq>\*(Aq, "$file.new" or die "Can\*(Aqt write new file: $!";\&\& print "# Add this line to the top\en"; # <\-\-\- HERE\*(AqS THE MAGIC\&\& while( <$in> )\& {\& print $out $_;\& }\&\& close $out;.Ve.PPTo change existing lines, insert the code to modify the lines insidethe \f(CW\*(C`while\*(C'\fR loop. In this case, the code finds all lowercasedversions of \*(L"perl\*(R" and uppercases them. The happens for every line, sobe sure that you're supposed to do that on every line!.PP.Vb 2\& open my $in, \*(Aq<\*(Aq, $file or die "Can\*(Aqt read old file: $!";\& open my $out, \*(Aq>\*(Aq, "$file.new" or die "Can\*(Aqt write new file: $!";\&\& print "# Add this line to the top\en";\&\& while( <$in> )\& {\& s/\eb(perl)\eb/Perl/g;\& print $out $_;\& }\&\& close $out;.Ve.PPTo change only a particular line, the input line number, \f(CW$.\fR, isuseful. First read and print the lines up to the one you want tochange. Next, read the single line you want to change, change it, andprint it. After that, read the rest of the lines and print those:.PP.Vb 5\& while( <$in> ) # print the lines before the change\& {\& print $out $_;\& last if $. == 4; # line number before change\& }\&\& my $line = <$in>;\& $line =~ s/\eb(perl)\eb/Perl/g;\& print $out $line;\&\& while( <$in> ) # print the rest of the lines\& {\& print $out $_;\& }.Ve.PPTo skip lines, use the looping controls. The \f(CW\*(C`next\*(C'\fR in this exampleskips comment lines, and the \f(CW\*(C`last\*(C'\fR stops all processing once itencounters either \f(CW\*(C`_\|_END_\|_\*(C'\fR or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR..PP.Vb 6\& while( <$in> )\& {\& next if /^\es+#/; # skip comment lines\& last if /^_\|_(END|DATA)_\|_$/; # stop at end of code marker\& print $out $_;\& }.Ve.PPDo the same sort of thing to delete a particular line by using \f(CW\*(C`next\*(C'\fRto skip the lines you don't want to show up in the output. Thisexample skips every fifth line:.PP.Vb 5\& while( <$in> )\& {\& next unless $. % 5;\& print $out $_;\& }.Ve.PPIf, for some odd reason, you really want to see the whole file at oncerather than processing line by line, you can slurp it in (as long asyou can fit the whole thing in memory!):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -