O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A.10. Chapter 11

  1. Here's one way to do it:

    open(PW,"/etc/passwd") || die "How did you get logged in?";
    while (<PW>) {
        ($user,$uid,$gcos) = (split /:/)[0,2,4];
        ($real) = split /,/,$gcos;
        write;
    }
    format STDOUT =
    @<<<<<<< @>>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $user, $uid, $real
    .

    The first line opens the password file. The while loop processes the password file line by line. Each line is torn apart (with colon delimiters), loading up the scalar variables. The real name of the user is pulled out of the GCOS field. 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 =
    Username User ID Real Name
    ======== ======= =========
    .

    All it takes 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 @<<<
    $%
    
    Username User ID Real Name
    ======== ======= =========
    .

    Well, here again, to get stuff ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required