Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create formatted output?

Little tricks

How do I create formatted output?

by  flawless69  Posted    (Edited  )
I've seen the question of outputting formated text coming up a lot lately. So let's see if this helps.

The best way to really get a good layout is to use what perl calls a format. Think of this as a form for laying out text in a certain way.

The following example will line up the subject line with the telephone number. This is a very simple example but it can become much more complicated.

Code:
$subject = "Test form";
$info    = "Some miscellaneous information";
$info   .= " about me.";
$phone   = "555-5555";

write;

format STDOUT_TOP =
# This area can contain headers which can look like the 
# info between the periods below.
.
format STDOUT =
Subject:   @<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<
           $subject                     $info
Telephone: @<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<
           $phone                       $info
~                                       ^<<<<<<<<<<<<<<<<<<<
                                        $info
.

Note the period delimiters. They are used to enclose the actual form. The write method writes out the format. In this case the output is to STDOUT but any file handle may be used. The "<" represents the characters. < will be left justified, > will be right justified, and | will be centered.
The "@" sign is the normal input beginning. The "^" can also be used as a way to create multiline text blocks (i.e. a long string with each line beginning at the ^). By using the "^" with short input, it is possible to have blank lines. By beginning the line with a "~" the line is not printed if it is blank. It is important to note that any characters running past the number of "<" (or ">" or "|") will be truncated.

Output of above code:
Code:
Subject:   Test form                    Some miscellaneous
Telephone: 555-5555                     information about
                                        me.
This is the basics of formats.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top