Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: A.9 Chapter 10, Filehandles and File TestsAppendix A
Exercise Answers
Next: A.11 Chapter 12, Directory Access
 

A.10 Chapter 11, Formats

  1. Here's one way to do it:

    $file = shift || die "usage: $0 filename";
    open(F, $file) || die "open: $!";
    while (<F>) {
      ($user, $company, $email) = split /:/;
      write;
    }
    format STDOUT =
    @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<
    $user,          $company,                $email
    .
    

    The second line opens the file. The while loop processes the file line-by-line. Each line is torn apart (with colon delimiters), which loads up the scalar variables. The final statement of the while loop invokes write to display all of the data.

    The format for the STDOUT filehandle defines a simple line with three fields. The values come from the three scalar variables that are given values in the while loop.

  2. Here's one way to do it:

    # append to program from the first problem...
    format STDOUT_TOP =
    User            Company                  Real Name
    ==============  =======================  =================
    .

    All you need to get page headers for the previous program is to add a top-of-page format. Here, we put column headers on the columns.

    To get the columns to line up, we copied the text of format STDOUT and used overstrike mode in our text editor to replace @<<< fields with ==== bars. That's the nice thing about the one-character-to-one-character correspondence between a format and the resulting display.

  3. Here's one way to do it:

    # append to program from the first problem...
    format STDOUT_TOP =
    Page @<<<
    $%
    
    User            Company                  Real Name
    ==============  =======================  =================
    .

    Well, here again, to get stuff at the top of the page, we've added a top-of-page format. This format also contains a reference to $%, which gives us a page number automatically.


Previous: A.9 Chapter 10, Filehandles and File TestsLearning Perl on Win32 SystemsNext: A.11 Chapter 12, Directory Access
A.9 Chapter 10, Filehandles and File TestsBook IndexA.11 Chapter 12, Directory Access