Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Little tricks

How do I create formatted output? by flawless69
Posted: 26 Jun 02

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.

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


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
~                                       ^<<<<<<<<<<<<<<<<<<<

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:

Subject:   Test form                    Some miscellaneous
Telephone: 555-5555                     information about

This is the basics of formats.  

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close