This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
allow evals to see the full lexical scope
[perl5.git] / pod / perlform.pod
index fcdea24..fac7efc 100644 (file)
@@ -5,23 +5,23 @@ perlform - Perl formats
 =head1 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 on a page, what page you're on, when to
-print page headers, etc.  Keywords are borrowed from FORTRAN:
-format() to declare and write() to execute; see their entries in
-L<perlfunc>.  Fortunately, the layout is much more legible, more like
-BASIC's PRINT USING statement.  Think of it as a poor man's nroff(1).
-
-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 "types" in Perl.  This means that if you have a function
-named "Foo", it is not the same thing as having a format named "Foo".
-However, the default name for the format associated with a given
+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 FORTRAN: format() to declare and write()
+to execute; see their entries in L<perlfunc>.  Fortunately, the layout is
+much more legible, more like BASIC's PRINT USING statement.  Think of it
+as a poor man's nroff(1).
+
+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 "types" in Perl.  This means that if you have a
+function named "Foo", it is not the same thing as having a format named
+"Foo".  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 STDOUT is name "STDOUT", and the default format for filehandle
-TEMP is name "TEMP".  They just look the same.  They aren't.
+format for STDOUT is named "STDOUT", and the default format for filehandle
+TEMP is named "TEMP".  They just look the same.  They aren't.
 
 Output record formats are declared as follows:
 
@@ -30,7 +30,7 @@ Output record formats are declared as follows:
     .
 
 If name is omitted, format "STDOUT" is defined.  FORMLIST consists of
-a sequence of lines, each of which may be of one of three types:
+a sequence of lines, each of which may be one of three types:
 
 =over 4
 
@@ -60,11 +60,13 @@ justification, or centering.  If the variable would exceed the width
 specified, it is truncated.
 
 As an alternate form of right justification, you may also use "#"
-characters (with an optional ".") to specify a numeric field.  This way
-you can line up the decimal points.  If any value supplied for these
-fields contains a newline, only the text up to the newline is printed.
-Finally, the special field "@*" can be used for printing multi-line,
-non-truncated values; it should appear by itself on a line.
+characters (with an optional ".") to specify a numeric field. This way
+you can line up the decimal points. With a "0" (zero) instead of the
+first "#", 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
+"@*" can be used for printing multi-line, nontruncated values; it
+should appear by itself on a line.
 
 The values are specified on the following line in the same order as
 the picture fields.  The expressions providing the values should be
@@ -105,7 +107,7 @@ 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!)
 
-Top-of-form processing is by default handled by a format with the 
+Top-of-form processing is by default handled by a format with the
 same name as the current filehandle with "_TOP" concatenated to it.
 It's triggered at the top of each page.  See L<perlfunc/write>.
 
@@ -169,7 +171,7 @@ the first) is stored in C<$^L> (C<$FORMAT_FORMFEED>).  These variables are
 set on a per-filehandle basis, so you'll need to select() into a different
 one to affect them:
 
-    select((select(OUTF), 
+    select((select(OUTF),
            $~ = "My_Other_Format",
            $^ = "My_Top_Format"
           )[0]);
@@ -187,14 +189,14 @@ stage in the expression to single-step the debugger through):
 
 If you use the English module, you can even read the variable names:
 
-    use English;
+    use English '-no_match_vars';
     $ofh = select(OUTF);
     $FORMAT_NAME     = "My_Other_Format";
     $FORMAT_TOP_NAME = "My_Top_Format";
     select($ofh);
 
 But you still have those funny select()s.  So just use the FileHandle
-module.  Now, you can access these special variables using lower-case
+module.  Now, you can access these special variables using lowercase
 method names instead:
 
     use FileHandle;
@@ -205,25 +207,25 @@ Much better!
 
 =head1 NOTES
 
-Because the values line may contain arbitrary expressions (for at fields, 
+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 sprintf() or one of your own.  For example:
 
-    format Ident = 
+    format Ident =
        @<<<<<<<<<<<<<<<
        &commify($n)
     .
 
 To get a real at or caret into the field, do this:
 
-    format Ident = 
+    format Ident =
     I have an @ here.
            "@"
     .
 
 To center a whole line of text, do something like this:
 
-    format Ident = 
+    format Ident =
     @|||||||||||||||||||||||||||||||||||||||||||||||
            "Some text line"
     .
@@ -233,19 +235,19 @@ of the page, however wide it is."  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 eval() it:
 
-    $format  = "format STDOUT = \n";
-             . '^' . '<' x $cols . "\n";
-             . '$entry' . "\n";
-             . "\t^" . "<" x ($cols-8) . "~~\n";
-             . '$entry' . "\n";
+    $format  = "format STDOUT = \n"
+             . '^' . '<' x $cols . "\n"
+             . '$entry' . "\n"
+             . "\t^" . "<" x ($cols-8) . "~~\n"
+             . '$entry' . "\n"
              . ".\n";
     print $format if $Debugging;
-    eval $format; 
+    eval $format;
     die $@ if $@;
 
 Which would generate a format looking something like this:
 
- format STDOUT = 
+ format STDOUT =
  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  $entry
          ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
@@ -254,7 +256,7 @@ Which would generate a format looking something like this:
 
 Here's a little program that's somewhat like fmt(1):
 
- format = 
+ format =
  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
  $_
 
@@ -264,7 +266,7 @@ Here's a little program that's somewhat like fmt(1):
  while (<>) {
      s/\s*\n\s*/ /g;
      write;
- } 
+ }
 
 =head2 Footers
 
@@ -277,10 +279,10 @@ Here's one strategy:  If you have a fixed-size footer, you can get footers
 by checking $FORMAT_LINES_LEFT before each write() and print the footer
 yourself if necessary.
 
-Here's another strategy; open a pipe to yourself, using C<open(MESELF, "|-")> 
-(see L<perlfunc/open()>) and always write() to MESELF instead of
-STDOUT.  Have your child process massage its STDIN to rearrange
-headers and footers however you like.  Not very convenient, but doable.
+Here's another strategy: Open a pipe to yourself, using C<open(MYSELF, "|-")>
+(see L<perlfunc/open()>) and always write() to MYSELF instead of STDOUT.
+Have your child process massage its STDIN to rearrange headers and footers
+however you like.  Not very convenient, but doable.
 
 =head2 Accessing Formatting Internals
 
@@ -295,7 +297,7 @@ For example:
 
     print "Wow, I just stored `$^A' in the accumulator!\n";
 
-Or to make an swrite() subroutine which is to write() what sprintf()
+Or to make an swrite() subroutine, which is to write() what sprintf()
 is to printf(), do this:
 
     use Carp;
@@ -305,7 +307,7 @@ is to printf(), do this:
        $^A = "";
        formline($format,@_);
        return $^A;
-    } 
+    }
 
     $string = swrite(<<'END', 1, 2, 3);
  Check me out
@@ -315,18 +317,18 @@ is to printf(), do this:
 
 =head1 WARNINGS
 
-The lone dot that ends a format can also prematurely end an E-Mail
+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 E-Mail, you should indent it so that
+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
-E-Mail cutoff.
+SMTP cutoff.
 
 Lexical variables (declared with "my") 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.)
 
-Formats are the only part of Perl which unconditionally use information
+Formats are the only part of Perl that unconditionally use information
 from a program's locale; if a program's environment specifies an
 LC_NUMERIC locale, it is always used to specify the decimal point
 character in formatted output.  Perl ignores all other aspects of locale
@@ -335,3 +337,12 @@ cannot be controlled by C<use locale> because the pragma is tied to the
 block structure of the program, and, for historical reasons, formats
 exist outside that block structure.  See L<perllocale> for further
 discussion of locale handling.
+
+Inside of an expression, the whitespace characters \n, \t and \f are
+considered to be equivalent to a single space.  Thus, you could think
+of this filter being applied to each value in the format:
+
+ $value =~ tr/\n\t\f/ /;
+
+The remaining whitespace character, \r, forces the printing of a new
+line if allowed by the picture line.