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

Hello Monks,

I'm a bit curious to know if there are any monks that actually use format and write in perl to actually format their output on a regular basis?

I would imagine that formats have some uses. However, it seems in larger scripts where you'd want to be using strict and avoiding global variables, they wouldn't be so attractive.

Are there tricks to make this feature more useful, or is this really just something with a limited scope of usability?

Replies are listed 'Best First'.
(jeffa) Re: Are formats in perl useful?
by jeffa (Bishop) on Mar 30, 2002 at 18:11 UTC
    What are formats good for? Text reports.

    I don't use formats a lot, but i do still use them. I suppose Text::Template or printf/sprintf could do the trick as well, but sometimes formats just are the natural thing to do. Besides, even if no-one else used formats, that would not stop me from using them if it was the right solution. You can still use strict with formats, but you are right about the globals ... however, with proper planning you can still achieve a robust maintainable script that uses formats. I like to declare my format variables with use vars, such as:

    use vars qw($foo $bar $baz);
    UPDATE:
    But, thanks to Dominus, runrig, and Kanji, i know now a better way - scroll on down and read their comments. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Are formats in perl useful?
by Flame (Deacon) on Mar 30, 2002 at 18:29 UTC
    I can't speak for anyone else, but I use formats with email reports in some of my programs. It just makes them more readable.

    A longstanding problem I had was the fact that you can't use things like IO::String or any other tied filehandle usually. However, as I learned in format and IO::String, there are ways to keep it in memory. My prefered method: IO::File->new_tmpfile() because it does permit the normal usage of format declarations.

    Just sharing what others shared with me. Good luck.



    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

Re: Are formats in perl useful?
by erikharrison (Deacon) on Mar 30, 2002 at 21:16 UTC

    Sure, formats are useful! They fill in a little niche between short scripts (where printf is about all you need) and application level software (where a templating system is a better idea).

    But to round out a discussion of formats, it must be remembered that formats have become less useful over the years, as the existence of Perl ( and the existence of perl modules) has led to the ability to have robust, widespread templating systems, which can do all that formats do (and more), and you don't even have to compile them if you don't want to.

    Historically, Perl has been extra careful about backwards compatibility. What worked in Perl 1 generally works in Perl 5.6. With Perl 6, this will change slightly. Another change in Perl 6, Larry has decided that formats don't belong in the core, but instead should be made a module, so that 5.X programs which use formats can simply add use Text::Format (or the moral equivalent) and not change signifigant portions of code.

    Cheers,
    Erik
Re: Are formats in perl useful?
by tmiklas (Hermit) on Mar 30, 2002 at 20:18 UTC
    Well - i use formats as well as any other templating system. Everything depends on what i'm actually trying to do - if it's a some documment (file) with some information prepared dynamically i use the templating modules. If it's a simple task i use formats.
    Simplicity - that's all. Formats are really great when you have to prepare some reports or something like this - when the documment consists of many *almost identical* lines, and that's what i use formats for ;-)

    Greetz, Tom.
Re: Are formats in perl useful?
by Dominus (Parson) on Mar 31, 2002 at 05:04 UTC
    Says ehdonhon:
    Are there tricks to make this feature more useful...?
    Instead of using format and write, use formline and $^A. That gets you around the strict vars problems, and it's more flexible. It also avoids having to mess around with $^ and $~.

    --
    Mark Dominus
    Perl Paraphernalia

      Yeah, I was so happy about finding out about formline and $^A, and so few (yet knowledgeable) monks (that were online) at the time seemed to know about them, that I wrote a node about it :-)
      ----------
      ooo  O\O  ooo tilly was here :,(