Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Do you use format and write?

by pernod (Chaplain)
on Mar 10, 2005 at 15:23 UTC ( #438287=perlquestion: print w/replies, xml ) Need Help??

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


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 | | 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 :)

Mischief. Mayhem. Soap.

Replies are listed 'Best First'.
Re: Do you use format and write?
by VSarkiss (Monsignor) on Mar 10, 2005 at 15:34 UTC

      I think those are two good points, but I think part of it is also just mindset of the programmers. Perl has a whole lot of functions that do similar things, as it borrows from many different languages. For instance, someone who's only dealt with perl might wonder why someone would ever do:

      $string = join ( $replace, split ( $search, $string ) );

      Which is roughly equivalent to:

      $string =~ s/$search/$replace/g;

      If you came from a language without regex matching, it makes perfect sense, though. You can use a person's choice of text output method to try to guess their background -- if you have almost all of the output coming from write, I'd assume that the programmer has a Fortran or similar background. if it's almost exclusively printf, then I'd assume they came from C. If it's all concatination without string interpolation, I'd assume something like JavaScript. I'm not going to be right every time, of course, but it seems that your thought processes about how to get things done is influenced by what tools to had available (or what tools you knew you had available, and you thought you knew how to use).

      Personally, I did my time with Fortran, and I like the power of formats in it... but I don't think I've ever used write in perl. I'm more likely to use printf. One of these days, I'll actually get around to implementing a (s)printf that supports the LPC extensions like column mode, table mode, and arrays.

      Oh -- and for 'nicely formatted' HTML, there's always preformatted text. After implementing it, the preformatted versions got more hits than the HTML table versions. (they even gave up on the table formatting ... too bad I never got preformatted promoted above the paragraph style)

Re: Do you use format and write?
by RazorbladeBidet (Friar) on Mar 10, 2005 at 15:31 UTC
    When you think that format is only a means of output, it's just one of many tools. There are many questions about HTML, XML, etc. output.

    I think the trend away from format is that nobody really cares about text output anymore in a pretty format. They want gimmicks, images, bright flashing lights and all the latest bells and whistles

    That said, I use format for text email reports I send out. I just think there's really not much to it - at least not as much as the "extraction" part ;)
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: Do you use format and write?
by Roy Johnson (Monsignor) on Mar 10, 2005 at 15:48 UTC
    I think the reason we don't see a lot about formats is that they're pretty mundane. Not a lot of questions to be asked about them.

    The interesting thought that your post gave me was this: formats are no longer worth including in core perl. By that, I mean that if the feature were being considered today, people would say "write a module for that -- it shouldn't be part of the core language." It would very probably be a core module, but not part of the language itself.

    I notice that Perl 6 still includes formats. I wonder if that's because nobody ever considered dropping them?
    Correction: formatting is done by a module in Perl 6.

    Caution: Contents may have been coded under pressure.

      I suspect that it's because under Perl6, they've been given new features that make them relevant to a larger portion of Perl's users again.


Re: Do you use format and write?
by shockme (Chaplain) on Mar 10, 2005 at 15:51 UTC
    I think RazorbladeBidet and VSarkiss summed up the reasons fairly well. However, from a sysadmin point of view, I use it quite often for formatting text to the screen. Of course, the scripts are usually my homegrown diagnostic tools and I only use format because other sysadmins around the office use these scripts as well. They like it purty. And, like RazorbladeBidet stated, it's handy for generating email as well.

    All in all, I think it's still used. The lack of questions may be due to the fact that there is so little that can go wrong with it.

    If things get any worse, I'll have to ask you to stop helping me.

Re: Do you use format and write?
by Zed_Lopez (Chaplain) on Mar 11, 2005 at 19:23 UTC
    Format fans should note that Perl6::Form doesn't use source filters and doesn't have the scary disclaimers that pretty much all of the rest of the Perl6 modules have -- I'd be willing to use it in production code today (but haven't.)
You bet, and for the same thing as you..
by bcole23 (Scribe) on Mar 11, 2005 at 15:30 UTC
    I agree with what the previous monks have said. I use it for tools and being a dba/admin, I have many tools where it is very useful for generating output. For ex:
    sub print_logcon { my ($which,$f1,$f2,$f3,$f4) = @_; if ($which eq 'general') { print "$f1"; print LOG_FILE "$f1" or kill_and_clean_up( "Error printing to +$LOG_FILE: $!" ); } elsif ($which eq 'comparison') { my $f1_multi = $f1; format COMP_TOP = File version comparison Type File name Previous New ---------------------------------------------------------------------- +-------- . format COMP = @<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<< +<<<<<<<< $f4, $f1, $f2, $f3 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $f1 . select(LOG_FILE); $^ = 'COMP_TOP'; $~ = 'COMP'; write LOG_FILE or kill_and_clean_up( "Error printing to $LOG_FILE: + $!" ); $f1 = $f1_multi; # format destroys the variable, so reassign it select(STDOUT); $^ = 'COMP_TOP'; $~ = 'COMP'; write STDOUT; } elsif ($which eq 'file_info') { my $f2_multi = $f2; format FILE_INFO_TOP = File Compilation Information Type File name Version ---------------------------------------------------------------------- +-------- . format FILE_INFO = @<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<< +<<<<<<<<< $f1, $f2, $f3 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $f2 . select(LOG_FILE); $^ = 'FILE_INFO_TOP'; $~ = 'FILE_INFO'; write LOG_FILE or kill_and_clean_up( "Error printing to $LOG_FILE: + $!" ); $f2 = $f2_multi; # format destroys the variable, so reassign it select(STDOUT); $^ = 'FILE_INFO_TOP'; $~ = 'FILE_INFO'; write STDOUT; } }
Re: Do you use format and write?
by rahed (Scribe) on Mar 13, 2005 at 17:15 UTC

    I used format and write just recently for email reports. I also wondered why there is no mention about format in newsgroups these days.

    The reports were distributed through MS Exchange. I like flat text simplicity, it was formatted into tables with equals and hyphen-minus signs. But after some time I replaced it with html. Sometimes the mentioned signs were narrower than other characters which destroyed the layout and the MS client had to be restarted to refresh the formatting.

    And of course colored text for most people is more attractive.

    I encountered one problem with the format. When centering the text one is supposed to use a verical bar in place of text. But at the same time I wanted to use the bar for column separation. So I had to insert a space at the end of text just before the separator to discern 'text bar' and 'column bar'. The solution is annoying for columns with small width.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438287]
Approved by BazB
Front-paged by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-10-01 03:01 GMT
Find Nodes?
    Voting Booth?

    No recent polls found