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 fileformat STDOUT_TOP = Passwd FileName Login Office Uid Gid Home------------------------------------------------------------------. format STDOUT =@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<$name, $login, $office,$uid,$gid, $home. $= = 20;# sets 20 lines per pagewhile (<>) {( $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 FileName Login Office Uid Gid Home------------------------------------------------------------------nobody * -2 -2 Unpri /var/emptyroot * 0 0 Syste /var/rootdaemon * 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 FileName 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 changes02/08/2008:Maintenance02/16/2008:Major problems. Disk crash, all work lost02/17/2008:Starting over02/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/perlformat 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 changes02/08/200 Maintenance02/16/200 Major problems. Disk crash, all work lost02/17/200 Starting over02/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.
ไม่มีความคิดเห็น:
แสดงความคิดเห็น