You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Chetan Patil <cp...@iescrow.com> on 2000/07/30 00:26:02 UTC

Overriding print in cgi scripts

Hello,
We have a large code base printing header, body and footers to the STDOUT
from the cgi scripts. Since all these are print statements, we have to go to
extreme measures to make sure we donot print the content more than once.
To clean things up (or at least start at it), we want to be able to trap the
output of print to a local buffer and then selectively flush or junk based
on the logic (for eg, if we decide to redirect at the end of the script,
then we want to junk the buffer).
I am looking for a quick solution which would allow me to override the
CORE::print function and simply write to a buffer (a perl scalar). Once I am
done with the processing, I can write out the buffer or junk it.
Is it possible to do this? What should I be careful of?

THanks,
Chetan



Re: Overriding print in cgi scripts

Posted by darren chamberlain <da...@boston.com>.
Chetan Patil (cpatil@iescrow.com) said something to this effect:
> Hello,
> We have a large code base printing header, body and footers to the STDOUT
> from the cgi scripts. Since all these are print statements, we have to go to
> extreme measures to make sure we donot print the content more than once.
> To clean things up (or at least start at it), we want to be able to trap the
> output of print to a local buffer and then selectively flush or junk based
> on the logic (for eg, if we decide to redirect at the end of the script,
> then we want to junk the buffer).
> I am looking for a quick solution which would allow me to override the
> CORE::print function and simply write to a buffer (a perl scalar). Once I am
> done with the processing, I can write out the buffer or junk it.
> Is it possible to do this? What should I be careful of?

How about selecting a different filehandle than STDOUT? For example, an
IO::String object?

IO::String is great (find it on CPAN). Here's an example:

bash$ perl -MIO::String
my $f = '';        
my $io = IO::String->new($f);
my $oldfh= select $io;
print "Hello, World!\n";
select $oldfh;
print $f;
^D
Hello, World!
bash$

Create a new IO::String object at the beginning of each script, select it, and
then you don't have to modify your print statements. Just be sure to reselect
STDOUT (or $oldfh as above).

Beware, however: IO::String seems pretty expensive (it uses tie).

(darren)

-- 
There are worse things in life than death. Have you ever spent an
evening with an insurance salesman?
-- Woody Allen

Re: Overriding print in cgi scripts

Posted by Yann Kerhervé <yk...@cyberion.net>.
On Sat, Jul 29, 2000 at 03:26:02PM -0700, Chetan Patil wrote:
> Hello,
> We have a large code base printing header, body and footers to the STDOUT
> from the cgi scripts. Since all these are print statements, we have to go to
> extreme measures to make sure we donot print the content more than once.
> To clean things up (or at least start at it), we want to be able to trap the
> output of print to a local buffer and then selectively flush or junk based
> on the logic (for eg, if we decide to redirect at the end of the script,
> then we want to junk the buffer).
> I am looking for a quick solution which would allow me to override the
> CORE::print function and simply write to a buffer (a perl scalar). Once I am
> done with the processing, I can write out the buffer or junk it.
> Is it possible to do this? What should I be careful of?
> 

Perhaps Apache::Filter do the job ?

-- 
Y a n n   K e r h e r v é                            | yk@cyberion.net
Developer EdenJob, Webmaster Esiea-Ouest             | ICQ:   43893215
Perl Mongueur,  Linux,  Linux-Alpha, ...             | TEL: 0146217695


Re: Overriding print in cgi scripts

Posted by "Ken Y. Clark" <kc...@boston.com>.
On Sat, 29 Jul 2000, Chetan Patil wrote:

> Hello,
> We have a large code base printing header, body and footers to the STDOUT
> from the cgi scripts. Since all these are print statements, we have to go to
> extreme measures to make sure we donot print the content more than once.
> To clean things up (or at least start at it), we want to be able to trap the
> output of print to a local buffer and then selectively flush or junk based
> on the logic (for eg, if we decide to redirect at the end of the script,
> then we want to junk the buffer).
> I am looking for a quick solution which would allow me to override the
> CORE::print function and simply write to a buffer (a perl scalar). Once I am
> done with the processing, I can write out the buffer or junk it.
> Is it possible to do this? What should I be careful of?
> 
> THanks,
> Chetan
> 

chetan,

if you have to look into overridding perl's print function, you should
probably look into radically changing the structure of your program, imho.
if nothing else, build up the output you want to create and store it in a
scalar, then print that when you know it's ok.  better still, look into a
templating system (pls see threads from the last several days covering
this topic extensively) to divorce the output format from the logic of the
script.

ky