http://www.perlmonks.org?node_id=438287

pernod has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

This may be a bit rambling, but I was thinking about the Report part of Practical Extraction and Report Language today. I looked at perlform, and wondered why I don't see these features in use more often.

I started thinking about this after writing a small script that found the latest revision number of a set of .c and .h files and printed them to STDOUT. I ended up with this:

#! /usr/bin/perl use strict; use File::Find; my ( $filename, $revision, $date ); format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @>>>>>>>>> | @>>>>>>>>>> $filename, $revision, $date . find( \&wanted, "." ); sub wanted { if( /[hc]$/ ) { open( FILE, $_ ) or do { warn "Could not open ".$_; return }; $date = "Not Found"; $revision = "Not Found"; while( my $line = <FILE> ) { # This is the comment line I was after # * | A | 12.01.99 | developer@company.com | if( $line =~ /^\s*\*\s*\|\s*([A-Z])\s*\|\s*([^\s]+)/ ) { $revision = $1; $date = $2; } } $filename = $File::Find::name; write; close FILE; } }

It's a quick and dirty hack that uses File::Find to find all .c and .h files below the current directory, and a regular expression to fetch some fields from the header comments. The match results are then stuffed into the predefined values that are defined in the format, and then write pushes it all out to STDOUT.

Why is the use of format so seldom seen around here at the Monastery? Is it because of the rather arcane picture syntax, or perhaps because printf and friends are simpler to use? Or something else entirely?

I find that I like formats, though. Different. Declarative. Quite concise. Good :)

pernod
--
Mischief. Mayhem. Soap.