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

Dear Monks,
I have a website which gets all of its pages generated via CGI. Some pages require some heavy compilation only they rarely change. So I decided to implement some caching.

First thought was to rewrite the code, but it's a big application, and it requires editing data from the database, some pages contain perl code in pseudo tags which gets evaled and it might use print too, so I dropped this idea.

Second attempt was to cache output and set a page invalidation code in some places so that when somehting changes in a page the cache for it is recompiled on next page hit.

My idea was to capture STDOUT at the beginning onto a scalar if the page needs to be cached, and at the end of the script insert this data in the appropriate table and then release the scalar so I can print what I captured to the real STDOUT.

My attempts with Tie::Handle do not work. I probably misunderstood how this tying must work, here's what I've done so far, but it doesn't print the output to the test file, nor does it print back to the browser, the script just happily compiles with no errors and just exits with no output.

# at the start of cgi script my $output = ''; # to store the output. open SECOUT, ">&STDOUT"; tie *STDOUT, 'BLAH', $output; .... ...... .... # this is the last thing done before the script exits. open OUTFILE, ">>test.log" or die "Cannot open blah blah.. $!\n"; print OUTFILE "----\n", $output; close OUTFILE; print SECOUT $output; # reprint the output to real STDOUT ... .... .. # somewhere else. package BLAH; use Tie::Handle; my $data; sub TIEHANDLE { return bless {}, shift; } sub PRINT { my $self = shift; $data .= join '', @_; }

Any help with this? Another approach probably?


He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

Replies are listed 'Best First'.
Re: help with Tie::Handle
by steves (Curate) on Feb 22, 2003 at 20:14 UTC

    I'm not sure what that code is trying to do ... Tie::Handle is typically used as a base class, in which case your BLAH package should have this line after the use Tie::Handle; line:

    our @ISA = qw(Tie::Handle);
    But even with that it looks like you're trying to have BLAH save to a scalar maybe? You pass in $output but never save it anywhere in the BLAH object you create. You then save the print data to a lexical scalar named $data that only the BLAH package can see. Maybe you mean something more like this (not tested)?
    my $output = ''; # to store the output. open SECOUT, ">&STDOUT"; tie *STDOUT, 'BLAH', \$output; package BLAH; use Tie::Handle; our @ISA = qw(Tie::Handle); sub TIEHANDLE { my $class = shift; my $save_to = shift; return bless({SAVE_TO => $save_to}, class); } sub PRINT { my $self = shift; ${$self->{SAVE_TO}} .= join '', @_; }
    In other words, give the tie a reference to the scalar you want it to write to.

Re: help with Tie::Handle
by Aristotle (Chancellor) on Feb 22, 2003 at 21:17 UTC
    Mmm, you probably don't need to dup the filehandle - just untie it and print $output as normally.

    Makeshifts last the longest.