Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/1perl/perlform

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" 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.  | will give a
.\" real vertical bar.  \*(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-|\(bv\*(Tr
.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\}
.\"
.\" 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.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" 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 "PERLFORM 1"
.TH PERLFORM 1 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
perlform \- Perl formats
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Perl has a mechanism to help you generate simple reports and charts.  To
facilitate this, Perl helps you code up your output page close to how it
will look when it's printed.  It can keep track of things like how many
lines are on a page, what page you're on, when to print page headers,
etc.  Keywords are borrowed from \s-1FORTRAN:\s0 \fIformat()\fR to declare and \fIwrite()\fR
to execute; see their entries in perlfunc.  Fortunately, the layout is
much more legible, more like \s-1BASIC\s0's \s-1PRINT\s0 \s-1USING\s0 statement.  Think of it
as a poor man's \fInroff\fR\|(1).
.PP
Formats, like packages and subroutines, are declared rather than
executed, so they may occur at any point in your program.  (Usually it's
best to keep them all together though.) They have their own namespace
apart from all the other \*(L"types\*(R" in Perl.  This means that if you have a
function named \*(L"Foo\*(R", it is not the same thing as having a format named
\&\*(L"Foo\*(R".  However, the default name for the format associated with a given
filehandle is the same as the name of the filehandle.  Thus, the default
format for \s-1STDOUT\s0 is named \*(L"\s-1STDOUT\s0\*(R", and the default format for filehandle
\&\s-1TEMP\s0 is named \*(L"\s-1TEMP\s0\*(R".  They just look the same.  They aren't.
.PP
Output record formats are declared as follows:
.PP
.Vb 3
\&    format NAME =
\&    FORMLIST
\&    .
.Ve
.PP
If name is omitted, format \*(L"\s-1STDOUT\s0\*(R" is defined.  \s-1FORMLIST\s0 consists of
a sequence of lines, each of which may be one of three types:
.IP "1." 4
A comment, indicated by putting a '#' in the first column.
.IP "2." 4
A \*(L"picture\*(R" line giving the format for one output line.
.IP "3." 4
An argument line supplying values to plug into the previous picture line.
.PP
Picture lines are printed exactly as they look, except for certain fields
that substitute values into the line.  Each field in a picture line starts
with either \*(L"@\*(R" (at) or \*(L"^\*(R" (caret).  These lines do not undergo any kind
of variable interpolation.  The at field (not to be confused with the array
marker @) is the normal kind of field; the other kind, caret fields, are used
to do rudimentary multi-line text block filling.  The length of the field
is supplied by padding out the field with multiple "<\*(L", \*(R">\*(L", or \*(R"|"
characters to specify, respectively, left justification, right
justification, or centering.  If the variable would exceed the width
specified, it is truncated.
.PP
As an alternate form of right justification, you may also use \*(L"#\*(R"
characters (with an optional \*(L".\*(R") to specify a numeric field. This way
you can line up the decimal points. With a \*(L"0\*(R" (zero) instead of the
first \*(L"#\*(R", the formatted number will be padded with leading zeroes if
necessary. If any value supplied for these fields contains a newline,
only the text up to the newline is printed. Finally, the special field
\&\*(L"@*\*(R" can be used for printing multi\-line, nontruncated values; it
should appear by itself on a line.
.PP
The values are specified on the following line in the same order as
the picture fields.  The expressions providing the values should be
separated by commas.  The expressions are all evaluated in a list context
before the line is processed, so a single list expression could produce
multiple list elements.  The expressions may be spread out to more than
one line if enclosed in braces.  If so, the opening brace must be the first
token on the first line.  If an expression evaluates to a number with a
decimal part, and if the corresponding picture specifies that the decimal
part should appear in the output (that is, any picture except multiple \*(L"#\*(R"
characters \fBwithout\fR an embedded \*(L".\*(R"), the character used for the decimal
point is \fBalways\fR determined by the current \s-1LC_NUMERIC\s0 locale.  This
means that, if, for example, the run-time environment happens to specify a
German locale, \*(L",\*(R" will be used instead of the default \*(L".\*(R".  See
perllocale and \*(L"\s-1WARNINGS\s0\*(R" for more information.
.PP
Picture fields that begin with ^ rather than @ are treated specially.
With a # field, the field is blanked out if the value is undefined.  For
other field types, the caret enables a kind of fill mode.  Instead of an
arbitrary expression, the value supplied must be a scalar variable name
that contains a text string.  Perl puts as much text as it can into the
field, and then chops off the front of the string so that the next time
the variable is referenced, more of the text can be printed.  (Yes, this
means that the variable itself is altered during execution of the \fIwrite()\fR
call, and is not returned.)  Normally you would use a sequence of fields
in a vertical stack 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.  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.
.PP
Using caret fields can produce variable length records.  If the text
to be formatted is short, you can suppress blank lines by putting a
\&\*(L"~\*(R" (tilde) character anywhere in the line.  The tilde will be translated
to a space upon output.  If you put a second tilde contiguous to the
first, the line will be repeated until all the fields on the line are
exhausted.  (If you use a field of the at variety, the expression you
supply had better not give the same value every time forever!)
.PP
Top-of-form processing is by default handled by a format with the
same 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.
.PP
Examples:
.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
\& .
.Ve
.PP
.Vb 29
\& # 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
.PP
It is possible to intermix \fIprint()\fRs with \fIwrite()\fRs on the same output
channel, but you'll have to handle \f(CW\*(C`$\-\*(C'\fR (\f(CW$FORMAT_LINES_LEFT\fR)
yourself.
.Sh "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 (except
the first) is stored in \f(CW$^L\fR (\f(CW$FORMAT_FORMFEED\fR).  These variables are
set on a per-filehandle basis, so you'll need to \fIselect()\fR into a different
one to affect them:
.PP
.Vb 4
\&    select((select(OUTF),
\&            $~ = "My_Other_Format",
\&            $^ = "My_Top_Format"
\&           )[0]);
.Ve
.PP
Pretty ugly, eh?  It's a common idiom though, so don't be too surprised
when you see it.  You can at least use a temporary variable to hold
the previous filehandle: (this is a much better approach in general,
because not only does legibility improve, you now have intermediary
stage in the expression to single-step the debugger through):
.PP
.Vb 4
\&    $ofh = select(OUTF);
\&    $~ = "My_Other_Format";
\&    $^ = "My_Top_Format";
\&    select($ofh);
.Ve
.PP
If you use the English module, you can even read the variable names:
.PP
.Vb 5
\&    use English '-no_match_vars';
\&    $ofh = select(OUTF);
\&    $FORMAT_NAME     = "My_Other_Format";
\&    $FORMAT_TOP_NAME = "My_Top_Format";
\&    select($ofh);
.Ve
.PP
But you still have those funny \fIselect()\fRs.  So just use the FileHandle
module.  Now, you can access these special variables using lowercase
method names instead:
.PP
.Vb 3
\&    use FileHandle;
\&    format_name     OUTF "My_Other_Format";
\&    format_top_name OUTF "My_Top_Format";
.Ve
.PP
Much 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 processing
to other functions, like \fIsprintf()\fR or one of your own.  For example:
.PP
.Vb 4
\&    format Ident =
\&        @<<<<<<<<<<<<<<<
\&        &commify($n)
\&    .
.Ve
.PP
To get a real at or caret into the field, do this:
.PP
.Vb 4
\&    format Ident =
\&    I have an @ here.
\&            "@"
\&    .
.Ve
.PP
To center a whole line of text, do something like this:
.PP
.Vb 4
\&    format Ident =
\&    @|||||||||||||||||||||||||||||||||||||||||||||||
\&            "Some text line"
\&    .
.Ve
.PP
There is no builtin way to say \*(L"float this to the right hand side
of 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, based
on the current number of columns, and then \fIeval()\fR it:
.PP
.Vb 9
\&    $format  = "format STDOUT = \en"
\&             . '^' . '<' x $cols . "\en"
\&             . '$entry' . "\en"
\&             . "\et^" . "<" x ($cols-8) . "~~\en"
\&             . '$entry' . "\en"
\&             . ".\en";
\&    print $format if $Debugging;
\&    eval $format;
\&    die $@ if $@;
.Ve
.PP
Which would generate a format looking something like this:
.PP
.Vb 6
\& format STDOUT =
\& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\& $entry
\&         ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
\& $entry
\& .
.Ve
.PP
Here's a little program that's somewhat like \fIfmt\fR\|(1):
.PP
.Vb 3
\& format =
\& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
\& $_
.Ve
.PP
.Vb 1
\& .
.Ve
.PP
.Vb 5
\& $/ = '';
\& while (<>) {
\&     s/\es*\en\es*/ /g;
\&     write;
\& }
.Ve
.Sh "Footers"
.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 thing
for a footer.  Not knowing how big a format is going to be until you
evaluate it is one of the major problems.  It's on the \s-1TODO\s0 list.
.PP
Here's one strategy:  If you have a fixed-size footer, you can get footers
by checking \f(CW$FORMAT_LINES_LEFT\fR before each \fIwrite()\fR and print the footer
yourself if necessary.
.PP
Here'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 footers
however you like.  Not very convenient, but doable.
.Sh "Accessing Formatting Internals"
.IX Subsection "Accessing Formatting Internals"
For low-level access to the formatting mechanism.  you may use \fIformline()\fR
and access \f(CW$^A\fR (the \f(CW$ACCUMULATOR\fR variable) directly.
.PP
For example:
.PP
.Vb 3
\&    $str = formline <<'END', 1,2,3;
\&    @<<<  @|||  @>>>
\&    END
.Ve
.PP
.Vb 1
\&    print "Wow, I just stored `$^A' in the accumulator!\en";
.Ve
.PP
Or to make an \fIswrite()\fR subroutine, which is to \fIwrite()\fR what \fIsprintf()\fR
is 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;
\&    }
.Ve
.PP
.Vb 5
\&    $string = swrite(<<'END', 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 mail
message passing through a misconfigured Internet mailer (and based on
experience, such misconfiguration is the rule, not the exception).  So
when sending format code through mail, you should indent it so that
the format-ending dot is not on the left margin; this will prevent
\&\s-1SMTP\s0 cutoff.
.PP
Lexical variables (declared with \*(L"my\*(R") are not visible within a
format unless the format is declared within the scope of the lexical
variable.  (They weren't visible at all before version 5.001.)
.PP
Formats are the only part of Perl that unconditionally use information
from a program's locale; if a program's environment specifies an
\&\s-1LC_NUMERIC\s0 locale, it is always used to specify the decimal point
character in formatted output.  Perl ignores all other aspects of locale
handling unless the \f(CW\*(C`use locale\*(C'\fR pragma is in effect.  Formatted output
cannot be controlled by \f(CW\*(C`use locale\*(C'\fR because the pragma is tied to the
block structure of the program, and, for historical reasons, formats
exist outside that block structure.  See perllocale for further
discussion of locale handling.
.PP
Inside of an expression, the whitespace characters \en, \et and \ef are
considered to be equivalent to a single space.  Thus, you could think
of this filter being applied to each value in the format:
.PP
.Vb 1
\& $value =~ tr/\en\et\ef/ /;
.Ve
.PP
The remaining whitespace character, \er, forces the printing of a new
line if allowed by the picture line.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].