# # Perl reporting basics
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Perl Reporting

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© March 2008 Anthony Lawrence

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 produces 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.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Perl reporting basics


Inexpensive and informative Apple related e-books:

Photos for Mac: A Take Control Crash Course

Take Control of Pages

Photos: A Take Control Crash Course

Are Your Bits Flipped?

Take Control of IOS 11





More Articles by © Anthony Lawrence





Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





One in a million is next Tuesday. (Gordon Letwin)




Linux posts

Troubleshooting posts


This post tagged:

Basics

Perl

Programming



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode