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

Wisdom escapes me...as I have only just begun.

The scenario:

Got asked by my boss to translate a bunch of scripts form bash to PERL. Not a bad idea...only...the scripts are a nightmare and the program tree really could use some major pruning...we are talking a jungle being pruned back to a rosebush.

The main question I have:

I have reviewed a very poor style of inserting/formatting text within a bash script. It appears that several functions were made containing formatted text. These functions are then called when convenient to print the formated text on screen. (interactive shell scripts).

In trying to translate to PERL, I find this method inefficient, but I have not figured out a better way in PERL of doing the same...and by formatted I imply blank lines and tabbing. (not bold, italics, etc...).

The question:

Is there a lazy way, err...more wise way to output blocks of text to the stdout other than putting a block as a subroutine and calling subroutines in order as a means to "create" a pretty text readout on screen? I'd really like to utilize the same text with pod as well...so I can create help files as well as print the text on the screen when the script is run.

Any insight available is appreciated and elaboration upon request is possible.

Thank you,

-monkmoose

Replies are listed 'Best First'.
Re: Text in Scripts
by hardburn (Abbot) on Aug 30, 2004 at 21:11 UTC

    You can use a templating system. If your needs are simple, Text::BasicTemplate should get the job done.

    On a side point, in a recent application I wasn't allowed to use a templating system (for various political reasons), and used much the same subroutine-based solution you seem to be talking about. I'd usually have the sub return the string instead of printing it out, but I could see having it print out directly if the program is small.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Text in Scripts
by Plankton (Vicar) on Aug 30, 2004 at 21:12 UTC
    Why not post an example of this "very poor style of inserting/formatting text"? Are you talking about HERE DOCS? You can use those both in Perl and shell scripting ... an example of a HERE doc is ...
    $ cat <<HEREDOC > blah blah > > blah blha > HEREDOC blah blah + blah blha $ perl print <<HEREDOC; blah blha + blah blah HEREDOC ^D blah blha + blah blah $

    Plankton: 1% Evil, 99% Hot Gas.
Re: Text in Scripts
by eieio (Pilgrim) on Aug 30, 2004 at 21:16 UTC
    Perl formats may help depending on the formatting that you need to apply to the text (e.g., tables of data). I don't think this will address your POD requirement, however.
Re: Text in Scripts
by periapt (Hermit) on Aug 31, 2004 at 12:34 UTC
    Depending on the complexity of your output or the need for consistency over many scripts, you could use one of the templating modules or Perls internal formats. I've found Perls formats to pretty useful for short screen interaction. They give you a fair amount of control and good flexibility.

    The problem with formats is that they tend to be specific to the program/script for the most part. I suppose you could share them in a module or package but I've never tried that. However, if the scripts you are converting are self contained, that may not be a limitation.

    On the other hand, if you do have several scripts using screen outputs that are substantially the same, you might consider a templating module.

    Here is an paired down example of a Perl format that I use now. Just to give you an idea of what it looks like.
    sub GenerateRpt{ my $oldouthdl = select; $rtncd = 0; #initialize and gerate various variables here $Text::Wrap::columns = 40; select(STDOUT); # select(OUTFILE); to w +rite to a file $~ = "ERRRPT"; # $FORMAT_NAME $^ = "ERRRPT_TOP"; # $FORMAT_TOP_NAME $= = 81; # $FORMAT_LINES_PER_PAG +E $- = 0; # $FORMAT LINES_LEFT while(<INFILE>){ . # do somethings . . write; } # end while(<INFILE>) close(INFILE); select $oldouthdl; # restore original outp +ut file handle return $rtncd; #*****; format ERRRPT_TOP = @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +||||||||||| "$title01[$cat]" @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +||||||||||| "$title02" ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $cathdr[$cat] ~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $cathdr[$cat] ~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $cathdr[$cat] ~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $cathdr[$cat] ~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $cathdr[$cat] ---------------------------------------------------------------------- +----------- . format ERRRPT = Local Case Number: @<<<<<<<<<<<<<<<<<<< Seq: @<<< Crt: @< Ph +ase: @<<<<< $err[4], $err[5], $err[2], + $err[6] Err Number: @<<<<< Rec Number: @<<<<< Field Num +ber: @<<<<< $err[0], $err[1], $obfldnum +97{$err[8]} Field Information: Description: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $line01 ~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $line01 ~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< $line01 ---------------------------------------------------------------------- +----------- . } #end GenerateRpt()
    Good Lucl PJ

    PJ
    use strict; use warnings; use diagnostics; (if needed)
Re: Text in Scripts
by revdiablo (Prior) on Aug 31, 2004 at 16:56 UTC

    It appears your question has been answered, but one thing in your post has not yet been addressed. There is no such thing at PERL (unless you're talking about Acme::Inline::PERL). When we talk about the language, we say "Perl." When we talk about the program, we say "perl." When someone says "PERL," it only looks like they're shouting.