Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Useful usage of formats.

by dmitri (Curate)
on Apr 11, 2007 at 01:42 UTC ( #609272=perlquestion: print w/ replies, xml ) Need Help??
dmitri has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone use format anymore? And if so, for what? I was just reading perl591delta and I see that the formats are undergoing changes -- so someone must be using them somewhere.

Grepping my local /usr/lib/perl produces matches in Pod::Functions and Parse::RecDescent (this one uses it for error formatting). Anything else you know of?

In my close to a decade of Perl programming, I've never had an opportunity to use format for anything. Am I missing out?

Comment on Useful usage of formats.
Replies are listed 'Best First'.
Re: Useful usage of formats.
by graff (Chancellor) on Apr 11, 2007 at 04:08 UTC
    In my close to a decade of Perl programming, I've never had an opportunity to use format for anything. Am I missing out?

    Maybe not. ;-) I'm with you. In my 12 years of perl programming, I've never needed anything that goes beyond the scope of that old friend from my preceding 10 years with C: (s)printf.

    Maybe some of my code would be considered by some as "too complicated" or even "suboptimal" as a result, but it's really just a matter of what you happen to be comfortable with.

    (And Perl is that rarety among programming languages, where the choice of solution can be a matter of one's own comfort level, rather than being dictated by syntactic constraints or a limited library of available support functions.)

Re: Useful usage of formats.
by gloryhack (Deacon) on Apr 11, 2007 at 04:15 UTC
    I use format frequently, for creating nicely formatted outputs from reporting functions. Management types love their reports, after all, so there are a bazillion excuses to use formats. Formats are great for presenting columnar data.

    The application I'm working on now, in fact, is going to see format tomorrow when I plug in its reporting function. I was all set to use a template engine, but I'd feel bad if I didn't leave something interesting for the maintenance programmer to play with. Besides, it gets me out of truncating overlong strings before handing them off to a template engine.

    Yeah, you might be missing out.

Re: Useful usage of formats.
by polettix (Vicar) on Apr 11, 2007 at 09:49 UTC
    IIRC, formats will be tossed away from Perl 6, and their functionality will be provided by a module. TheDamian anticipated this functionality in a module you can use today from within Perl 5, namely Perl6::Form, that should be suitable for production-level scripts and modules according to Categorized Damian Modules. IMHO, this means that you can ignore formats today and stick to the module.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Useful usage of formats.
by shmem (Canon) on Apr 11, 2007 at 09:34 UTC
    I use format quite often to generate sort-of manual pages for interfaces, e.g. while programming perl frontends to SAP. Sample output of that particular usage:
    ====================================================================== +========== Interface RFC_READ_TABLE ====================================================================== +========== +----------------------------------------------------------+---------- +---------+ | TABLE | DATA | LEN 512| INTYPE + 4| | STRUCT | TAB512 | VALUE LEN + 0 | +--------+---------------------------------+------+--------+--------+- +---------+ | POS | NAME | LEN | INTYPE | OFFSET | +DECIMALS | +--------+---------------------------------+------+--------+--------+- +---------+ | 1 | WA | 512 | 0 | | + 0 | | --- | - Summe - | 512 | | | + | +----------------------------------------------------------+---------- +---------+ | TABLE | FIELDS | LEN 103| INTYPE + 4| | STRUCT | RFC_DB_FLD | VALUE LEN + 0 | +--------+---------------------------------+------+--------+--------+- +---------+ | POS | NAME | LEN | INTYPE | OFFSET | +DECIMALS | +--------+---------------------------------+------+--------+--------+- +---------+ | 1 | FIELDNAME | 30 | 0 | | + 0 | | 2 | OFFSET | 6 | 6 | 30 | + 0 | | 3 | LENGTH | 6 | 6 | 36 | + 0 | | 4 | TYPE | 1 | 0 | 42 | + 0 | | 5 | FIELDTEXT | 60 | 0 | 43 | + 0 | | --- | - Summe - | 103 | | | + | ... ====================================================================== +========== Parameter ====================================================================== +========== |------------------------------------+-------------------------------- +---------+ | PARAMETER DELIMITER | STRUCTURE (none) + | | VALUE | LEN | TYPE | INTYPE | DECIMALS | DEFA +ULT | | | 1 | IN | | 0 | + 0| |------------------------------------+-------------------------------- +---------+ | PARAMETER NO_DATA | STRUCTURE (none) + | | VALUE | LEN | TYPE | INTYPE | DECIMALS | DEFA +ULT | | | 1 | IN | | 0 | + 0|

    There's some ANSI color highlighting that I can't show in a <code> block. This lets me skim through an interface definition while developing whithout having to use the ugly SAP Frontend...

    Anyways, doing something like this with printf would be a pain. Same goes for all 'er' of Perl - 'Extraction and Report'. I use format for log file parsing output, mail body assembling, en fin for all stuff I'm loth to use a templating engine for.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Useful usage of formats.
by Anonymous Monk on Apr 11, 2007 at 03:56 UTC
    Damian uses format, so you must be missing out :)
      That's true :) But my error messages are just not as intricate as his...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://609272]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (17)
As of 2015-07-31 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (282 votes), past polls