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?
In reply to help with Tie::Handle by Chady
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |