On Mar 26, 5:53*pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> a better style i teach is called "print rarely, print late". instead of
> printing directly, return the string you make up.
In Paul Graham's book 'On Lisp' in chapter 3, 'Functional
Programming', he makes an extended case for this, which I find
compelling but not necessarily persuasive. I've been slowly making my
way through the dead tree version of HOP by MJD, and I also have found
a lot to like in that. My problems are (as Graham notes) an imperative
habit, a lack of opportunity, and little discretionary time.
> build up the final
> page in a buffer (using .= is easiest) and then you can decide what to
> do with it.
I actually use this quite a bit in my day job. I almost always find
myself constructing and deconstruction strings and arrays (and
hashes), moving between them, and as a result I do this quite a bit.
(I've also found the ||= operator useful.)
> you can print to stdout as usual, print to a file for later
> use by the web server, print to a socket if desired. or more than one of
> those at the same time. by printing directly from the html subs you lose
> the flexibility. it is also a bit faster to call print one time than
> multiple times (.= is faster than print). and you can decide where to
> print at the higher level which keeps the logic cleaner.
As an explanation if not a defense of what I wrote previously, most of
my HTML consists of front ends to databases, and most of the heavy
lifting is the SQL part. When I write my HTML files, I use variables
to trigger both the SQL and the HTML. I quite frequently end up with a
cgi script that looks something like this (illustration only):
HTML:

rint_header(@vars1);
HTML:

rint_banner(@vars2);
HTML:

rint_menu(@vars3);
my $hashref = SQL::get_list($var1);
HTML:

rint_content($hashref);
$hashref = SQL::get_calendar($var2);
HTML:

rint_content($hashref);
HTML:

rint_footer();
exit;
> i have seen
> requests for dual handles which can print to a file and to stdout (i
> think IO::Tee does that) but this is even easier and faster. so the rule
> print rarely means build up a full string in a buffer before you print
> it. print late means print only when you are done and can decide where
> to print it.
In this case, I am 'printing' a response to an HTTP request, not a
physical device. I also do some JSP, and for some reason, the JSP
doesn't seem any faster than my CGI, and sometimes is noticeably
slower.
CC