[comp.lang.perl] formatting to memory

raymond@math.berkeley.edu (Raymond Chen) (02/19/91)

I'd find it useful to be able to save the output of a write() into a
variable rather than sending it to the output channel.  The most
important example is taking advantage of the ^>>> format to let perl
do the hard work of paragraph-breaking.  Currently, I have a
subroutine that does the same thing, but I feel bad about reinventing
the wheel, not to mention the efficiency hit.  (Yes, I know I could
open a temp file, but I have to do this hundreds of times within a
single run of the program.)

For those who need to know why: Given two paragraphs (denoted below by
X's and Z's) and a rectangular array (denoted by Y's), I want to have
the text of the paragraph flow around the rectangular array, and the two
paragraphs started at a vertical offset from each other.  W's represent
other (fixed-field) information.

zzz wwwwwwww www wwwwwwwww wwww
zzz
zzz xxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy
zzz xxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The height and width of the rectangular array of Y's varies, so I have
a subroutine called &paraform($s, $w, $h) which splits $s into at most $h
lines of width $w; if $s requires more than $h lines, the unformatted
text is returns as the $h+1'th line.  So I call &paraform three times.
Once to format the Z's, again to format the first three lines of X's,
and a third time for format the last two lines of X's.  I then paste
all the little pieces together to produce the actual report.

There, now don't you wish you skipped my explanation of why I want
formatting to memory?

I wonder whether anybody else has wished for this facility.
--
$a="Just another perl wishlist maker,";&p;sub p{local($_)=chop$a;$a&&&p;print;}

tchrist@convex.COM (Tom Christiansen) (02/19/91)

From the keyboard of raymond@math.berkeley.edu (Raymond Chen):
:I'd find it useful to be able to save the output of a write() into a
:variable rather than sending it to the output channel.  
...
:I wonder whether anybody else has wished for this facility.

Yes, this would fix my trailing newline problem.

    chop($foo = swrite(fmt));

One snag I can foresee is that formats are bounds to filehandles.  Maybe
swrite() should just take an unadorned format name (or indirect one
through a scalar).

I see on Larry's wishlist to make format pictures niftier; besides
animation :-), what else can we come up with?

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)