📄 perlform.1
字号:
.IX Xref "format, fill mode".IX Subsection "Using Fill Mode"On text fields the caret enables a kind of fill mode. Instead of anarbitrary expression, the value supplied must be a scalar variablethat contains a text string. Perl puts the next portion of the text intothe field, and then chops off the front of the string so that the next timethe variable is referenced, more of the text can be printed. (Yes, thismeans that the variable itself is altered during execution of the \fIwrite()\fRcall, and is not restored.) The next portion of text is determined bya crude line breaking algorithm. You may use the carriage return character(\f(CW\*(C`\er\*(C'\fR) to force a line break. You can change which characters are legal to break on by changing the variable \f(CW$:\fR (that's \&\f(CW$FORMAT_LINE_BREAK_CHARACTERS\fR if you're using the English module) to a list of the desired characters..PPNormally you would use a sequence of fields in a vertical stack associated with the same scalar variable to print out a block of text. You might wish to end the final field with the text \*(L"...\*(R", which will appear in the output if the text was too long to appear in its entirety..Sh "Suppressing Lines Where All Fields Are Void".IX Xref "format, suppressing lines".IX Subsection "Suppressing Lines Where All Fields Are Void"Using caret fields can produce lines where all fields are blank. You cansuppress such lines by putting a \*(L"~\*(R" (tilde) character anywhere in theline. The tilde will be translated to a space upon output..Sh "Repeating Format Lines".IX Xref "format, repeating lines".IX Subsection "Repeating Format Lines"If you put two contiguous tilde characters \*(L"~~\*(R" anywhere into a line,the line will be repeated until all the fields on the line are exhausted,i.e. undefined. For special (caret) text fields this will occur sooner orlater, but if you use a text field of the at variety, the expression yousupply had better not give the same value every time forever! (\f(CW\*(C`shift(@f)\*(C'\fRis a simple example that would work.) Don't use a regular (at) numeric field in such lines, because it will never go blank..Sh "Top of Form Processing".IX Xref "format, top of form top header".IX Subsection "Top of Form Processing"Top-of-form processing is by default handled by a format with thesame name as the current filehandle with \*(L"_TOP\*(R" concatenated to it.It's triggered at the top of each page. See \*(L"write\*(R" in perlfunc..PPExamples:.PP.Vb 10\& # a report on the /etc/passwd file\& format STDOUT_TOP =\& Passwd File\& Name Login Office Uid Gid Home\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& .\& format STDOUT =\& @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<\& $name, $login, $office,$uid,$gid, $home\& .\&\&\& # a report from a bug report form\& format STDOUT_TOP =\& Bug Reports\& @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>>\& $system, $%, $date\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& .\& format STDOUT =\& Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $subject\& Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $index, $description\& Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $priority, $date, $description\& From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $from, $description\& Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $programmer, $description\& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $description\& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $description\& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $description\& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $description\& ~ ^<<<<<<<<<<<<<<<<<<<<<<<...\& $description\& ..Ve.PPIt is possible to intermix \fIprint()\fRs with \fIwrite()\fRs on the same outputchannel, but you'll have to handle \f(CW\*(C`$\-\*(C'\fR (\f(CW$FORMAT_LINES_LEFT\fR)yourself..Sh "Format Variables".IX Xref "format variables format, variables".IX Subsection "Format Variables"The current format name is stored in the variable \f(CW$~\fR (\f(CW$FORMAT_NAME\fR),and the current top of form format name is in \f(CW$^\fR (\f(CW$FORMAT_TOP_NAME\fR).The current output page number is stored in \f(CW$%\fR (\f(CW$FORMAT_PAGE_NUMBER\fR),and the number of lines on the page is in \f(CW$=\fR (\f(CW$FORMAT_LINES_PER_PAGE\fR).Whether to autoflush output on this handle is stored in \f(CW$|\fR(\f(CW$OUTPUT_AUTOFLUSH\fR). The string output before each top of page (exceptthe first) is stored in \f(CW$^L\fR (\f(CW$FORMAT_FORMFEED\fR). These variables areset on a per-filehandle basis, so you'll need to \fIselect()\fR into a differentone to affect them:.PP.Vb 4\& select((select(OUTF),\& $~ = "My_Other_Format",\& $^ = "My_Top_Format"\& )[0]);.Ve.PPPretty ugly, eh? It's a common idiom though, so don't be too surprisedwhen you see it. You can at least use a temporary variable to holdthe previous filehandle: (this is a much better approach in general,because not only does legibility improve, you now have intermediarystage in the expression to single-step the debugger through):.PP.Vb 4\& $ofh = select(OUTF);\& $~ = "My_Other_Format";\& $^ = "My_Top_Format";\& select($ofh);.Ve.PPIf you use the English module, you can even read the variable names:.PP.Vb 5\& use English \*(Aq\-no_match_vars\*(Aq;\& $ofh = select(OUTF);\& $FORMAT_NAME = "My_Other_Format";\& $FORMAT_TOP_NAME = "My_Top_Format";\& select($ofh);.Ve.PPBut you still have those funny \fIselect()\fRs. So just use the FileHandlemodule. Now, you can access these special variables using lowercasemethod names instead:.PP.Vb 3\& use FileHandle;\& format_name OUTF "My_Other_Format";\& format_top_name OUTF "My_Top_Format";.Ve.PPMuch better!.SH "NOTES".IX Header "NOTES"Because the values line may contain arbitrary expressions (for at fields,not caret fields), you can farm out more sophisticated processingto other functions, like \fIsprintf()\fR or one of your own. For example:.PP.Vb 4\& format Ident =\& @<<<<<<<<<<<<<<<\& &commify($n)\& ..Ve.PPTo get a real at or caret into the field, do this:.PP.Vb 4\& format Ident =\& I have an @ here.\& "@"\& ..Ve.PPTo center a whole line of text, do something like this:.PP.Vb 4\& format Ident =\& @|||||||||||||||||||||||||||||||||||||||||||||||\& "Some text line"\& ..Ve.PPThere is no builtin way to say \*(L"float this to the right hand sideof the page, however wide it is.\*(R" You have to specify where it goes.The truly desperate can generate their own format on the fly, basedon the current number of columns, and then \fIeval()\fR it:.PP.Vb 9\& $format = "format STDOUT = \en"\& . \*(Aq^\*(Aq . \*(Aq<\*(Aq x $cols . "\en"\& . \*(Aq$entry\*(Aq . "\en"\& . "\et^" . "<" x ($cols\-8) . "~~\en"\& . \*(Aq$entry\*(Aq . "\en"\& . ".\en";\& print $format if $Debugging;\& eval $format;\& die $@ if $@;.Ve.PPWhich would generate a format looking something like this:.PP.Vb 6\& format STDOUT =\& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\& $entry\& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~\& $entry\& ..Ve.PPHere's a little program that's somewhat like \fIfmt\fR\|(1):.PP.Vb 3\& format =\& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~\& $_\&\& .\&\& $/ = \*(Aq\*(Aq;\& while (<>) {\& s/\es*\en\es*/ /g;\& write;\& }.Ve.Sh "Footers".IX Xref "format, footer footer".IX Subsection "Footers"While \f(CW$FORMAT_TOP_NAME\fR contains the name of the current header format,there is no corresponding mechanism to automatically do the same thingfor a footer. Not knowing how big a format is going to be until youevaluate it is one of the major problems. It's on the \s-1TODO\s0 list..PPHere's one strategy: If you have a fixed-size footer, you can get footersby checking \f(CW$FORMAT_LINES_LEFT\fR before each \fIwrite()\fR and print the footeryourself if necessary..PPHere's another strategy: Open a pipe to yourself, using \f(CW\*(C`open(MYSELF, "|\-")\*(C'\fR(see \*(L"\fIopen()\fR\*(R" in perlfunc) and always \fIwrite()\fR to \s-1MYSELF\s0 instead of \s-1STDOUT\s0.Have your child process massage its \s-1STDIN\s0 to rearrange headers and footershowever you like. Not very convenient, but doable..Sh "Accessing Formatting Internals".IX Xref "format, internals".IX Subsection "Accessing Formatting Internals"For low-level access to the formatting mechanism. you may use \fIformline()\fRand access \f(CW$^A\fR (the \f(CW$ACCUMULATOR\fR variable) directly..PPFor example:.PP.Vb 3\& $str = formline <<\*(AqEND\*(Aq, 1,2,3;\& @<<< @||| @>>>\& END\&\& print "Wow, I just stored \`$^A\*(Aq in the accumulator!\en";.Ve.PPOr to make an \fIswrite()\fR subroutine, which is to \fIwrite()\fR what \fIsprintf()\fRis to \fIprintf()\fR, do this:.PP.Vb 8\& use Carp;\& sub swrite {\& croak "usage: swrite PICTURE ARGS" unless @_;\& my $format = shift;\& $^A = "";\& formline($format,@_);\& return $^A;\& }\&\& $string = swrite(<<\*(AqEND\*(Aq, 1, 2, 3);\& Check me out\& @<<< @||| @>>>\& END\& print $string;.Ve.SH "WARNINGS".IX Header "WARNINGS"The lone dot that ends a format can also prematurely end a mailmessage passing through a misconfigured Internet mailer (and based onexperience, such misconfiguration is the rule, not the exception). Sowhen sending format code through mail, you should indent it so thatthe format-ending dot is not on the left margin; this will prevent\&\s-1SMTP\s0 cutoff..PPLexical variables (declared with \*(L"my\*(R") are not visible within aformat unless the format is declared within the scope of the lexicalvariable. (They weren't visible at all before version 5.001.).PPFormats are the only part of Perl that unconditionally use informationfrom a program's locale; if a program's environment specifies an\&\s-1LC_NUMERIC\s0 locale, it is always used to specify the decimal pointcharacter in formatted output. Perl ignores all other aspects of localehandling unless the \f(CW\*(C`use locale\*(C'\fR pragma is in effect. Formatted outputcannot be controlled by \f(CW\*(C`use locale\*(C'\fR because the pragma is tied to theblock structure of the program, and, for historical reasons, formatsexist outside that block structure. See perllocale for furtherdiscussion of locale handling..PPWithin strings that are to be displayed in a fixed length text field,each control character is substituted by a space. (But remember thespecial meaning of \f(CW\*(C`\er\*(C'\fR when using fill mode.) This is done to avoidmisalignment when control characters \*(L"disappear\*(R" on some output media.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -