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

What is the cleanest way to send to STDOUT several different formats of text? Say, you have this legacy web report script, it is using printf to format the printing. This breaks occasionally, ie. not giving the desired format cleanly. Now, format I suppose would be an option, but I kinda think of it more as a way of formatting things for printing, not building an email or just going to STDOUT. Since there are also about 4 or 5 different formats being used within the script redefing format is rather a pain.. I guess Im just looking for suggestions really, how would you do it, if say you were building a new web report that required exacting formats, that would not break due to the input. tia

Replies are listed 'Best First'.
(jeffa) Re: clean formatting
by jeffa (Bishop) on Sep 29, 2000 at 19:27 UTC
    I think you need to look into format. Yes, it can be a pain, but it can also be very flexible. Since you are wanting several different formats, you can try:
    format = STDOUT; # for good ole standard out # insert format specifics here format = EMAIL; # user defined # insert format specifics here format = PRINTER; # etc
    I would like to be more specific, but I really need more info to go on, such as an example of what you are trying to format.

    Hope this helps,
    Jeff

      The format features in Perl are really a good thing to go for, even if they look like stone age compared to the politically correct OO stuff in common use today. A good thing to do is to hide the (ugly) interface to the format facility behind your own little wrapper functions.

      Another thing might be to do a CPAN search for "format" and look if you can use any of the modules there.

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

      I agree with jeffa's point here. If you have something simple, printf will probably suffice, but if you're going with multi-line chunks or full nicely formatted e-mails, Perl's format (perlform) abilities will handle this very nicely. You can have formats that span multiple lines, automatically do word wrapping for you, justification, etc. If you're dealing with plain-text data, there are few things more powerful and flexible for you

      It just takes a few minutes to learn.

Re: clean formatting
by AgentM (Curate) on Sep 29, 2000 at 19:21 UTC
    you probably want to make sure you're using CGI first so you don't hafta worry about the html tags. If you still need formatting, I prefer printf over FORMAT anyday, simply because it's more intuitive (or perhaps because I got used to it in C). printf() is not a deprecated but very useful function. I'm sure other people have opinions on FORMAT, though.
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.