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/

In reply to help with Tie::Handle by Chady

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.