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.
More Articles by Anthony Lawrence - Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
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. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar