ESN 52277-080312-439903-22


Document Name: Perl Reporting
Document Description: Perl reporting basics

Perl Reporting


2008/03/13

I often forget about Perl's reporting formats, and I suspect that a lot of people do - we're so used to Perl as a programming language that we forget it's original namesake: Practical Extraction and Report Language. Reporting was a big part of Perl's beginnings, but it's hardly ever mentioned today.

In fact, it's been so long since I have used any of these features that I had to drag out my big Camel Book to review the whole subject - I had forgotten everything. Fortunately, it's not hard, and using these capabilities can make output much easier: you don't have to keep track of lines printed so that you can generate headers on new pages or print page numbers: Perl does that for you. You can justify and center, easily handle variable width data and more. Perl Report Formats make all this easy.

Here's an example using a format suggested in the Camel book (or online at Perl Formats)

:
 #!/usr/bin/perl
 # a report on the /etc/passwd file
 format STDOUT_TOP =
                         Passwd File
 Name                Login    Office   Uid   Gid Home
 ------------------------------------------------------------------
 .
 
 format STDOUT =
 @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
 $name,              $login,  $office,$uid,$gid, $home
 .
 
 $= = 20;
 # sets 20 lines per page
 while (<>) {
 ( $name, $login,  $office,$uid,$gid, $home) =split /:/;
 next if not $login;
 write;
 
 }
 
 
 

On my Mac, running that program with /etc/passwd as its argument prodices this:

                         Passwd File
 Name                Login    Office   Uid   Gid Home
 ------------------------------------------------------------------
 nobody                 *     -2        -2 Unpri /var/empty
 root                   *     0          0 Syste /var/root
 daemon                 *     1          1 Syste /var/root
 _uucp                  *     4          4 Unix  /var/spool/uucp
 _lp                    *     26        26 Print /var/spool/cups
 _postfix               *     27        27 Postf /var/spool/postfix
 _mcxalr                *     54        54 MCX A /var/empty
 _pcastagent            *     55        55 Podca /var/pcast/agent
 _pcastserver           *     56        56 Podca /var/pcast/server
 _serialnumberd         *     58        58 Seria /var/empty
 _devdocs               *     59        59 Devel /var/empty
 _sandbox               *     60        60 Seatb /var/empty
 _mdnsresponder         *     65        65 mDNSR /var/empty
 _ard                   *     67        67 Apple /var/empty
 _www                   *     70        70 World /Library/WebServer
 _eppc                  *     71        71 Apple /var/empty
 _cvs                   *     72        72 CVS S /var/empty
 _svn                   *     73        73 SVN S /var/empty
 _mysql                 *     74        74 MySQL /var/empty
 _sshd                  *     75        75 sshd  /var/empty
 _qtss                  *     76        76 Quick /var/empty
 _cyrus                 *     77         6 Cyrus /var/imap
 _mailman               *     78        78 Mailm /var/empty
 _appserver             *     79        79 Appli /var/empty
 _clamav                *     82        82 ClamA /var/virusmails
 _amavisd               *     83        83 AMaVi /var/virusmails
 _jabber                *     84        84 Jabbe /var/empty
 _xgridcontroller       *     85        85 Xgrid /var/xgrid/control
 _xgridagent            *     86        86 Xgrid /var/xgrid/agent
 _appowner              *     87        87 Appli /var/empty
 _windowserver          *     88        88 Windo /var/empty
 _spotlight             *     89        89 Spotl /var/empty
 _tokend                *     91        91 Token /var/empty
 _securityagent         *     92        92 Secur /var/empty
 _calendar              *     93        93 Calen /var/empty
 _teamsserver           *     94        94 Teams /var/teamsserver
 _update_sharing        *     95        -2 Updat /var/empty
 
 ^L                      Passwd File
 Name                Login    Office   Uid   Gid Home
 ------------------------------------------------------------------
 _installer             *     96        -2 Insta /var/empty
 _atsserver             *     97        97 ATS S /var/empty
 _unknown               *     99        99 Unkno /var/empty
 

Ever had to handle variable length data in a report? That can be annoying, can't it? Let's say we have this text file:

 02/04/2008:No changes
 02/08/2008:Maintenance
 02/16/2008:Major problems.  Disk crash, all work lost
 02/17/2008:Starting over
 02/19/2008:This is just too much work.  The project is not worth the trouble. I quit.
 02/25/2008:Wow! Big raise.  Guess I don't quit.
 

Here's a little Perl script that eats that up:

 #!/usr/bin/perl
 format STDOUT =
 Date              Action
 @<<<<<<<<         ^<<<<<<<<<<<<<<<<<<<<
 $date             $action
 ~                 ^<<<<<<<<<<<<<<<<<<<<
                   $action
 ~                 ^<<<<<<<<<<<<<<<<<...
                   $action
 .
 
 while (<>) {
 ($date,$action)=split /:/;
 write;
 }
 

If we run that against our data file, we'll get:

 02/04/200         No changes
 02/08/200         Maintenance
 02/16/200         Major problems.  Disk
                   crash, all work lost
 02/17/200         Starting over
 02/19/200         This is just too much
                   work.  The project is
                   not worth the...
 02/25/200         Wow! Big raise.
                   Guess I don't quit.
 

There's a lot more to this, I just wanted to give you a taste here. You can read the docs and play with it yourself but can probably easily see how much time and trouble this could save you.


Author: Anthony Lawrence - Contact Author
Publisher: Anthony Lawrence
Licensee Name: Anthony Lawrence
Reference URL: http://aplawrence.com/Unixart/perl_reporting.html
Copyright: All Rights Reserved
Registration Date: 3/13/2008 12:07:52 AM UTC
Views: 926




NUMLY.COM