Format Variables
The current format name is stored in the variable $~ ($FORMAT_NAME), and the current top-of-form
format name is in $^ ($FORMAT_TOP_NAME). The current output page
number is stored in $% ($FORMAT_PAGE_NUMBER), and the number of lines
on the page is in $= ($FORMAT_LINES_PER_PAGE). Whether to flush the
output buffer on this handle automatically is stored in $| ($OUTPUT_AUTOFLUSH). The string to be output before each top of page (except
the first) is stored in $^L ($FORMAT_FORMFEED). These variables are set on
a per-filehandle basis, so you’ll need to select the filehandle associated with a format
in order to affect its format variables:
select((select(OUTF),
$~ = "My_Other_Format",
$^ = "My_Top_Format"
)[0]);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:
$ofh = select(OUTF); $~ = "My_Other_Format"; $^ = "My_Top_Format"; select($ofh);
This is a much better approach in general because not only does
legibility improve, but you now have an intermediary statement in the
code to stop on when you’re single-stepping in the debugger. If you use
the English module, you can even read the variable names:
use English; $ofh = select(OUTF); $FORMAT_NAME = "My_Other_Format"; $FORMAT_TOP_NAME = "My_Top_Format"; select($ofh);
But you still have those funny calls to select. If you want to avoid them, use the
IO::Handle module bundled with Perl. Now you can access these special ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access