I find myself in need of a way of redirecting/capturing STDOUT, but from within and program instead of from the command line.

In detail, here is the problem. We use a home grown templating system in which the template holds some perl and lots of html, with a bunch of $href->{somekey} entries.
That template is slurped up, and then eval'ed (with $href having been created/filled before the slurp and eval).

The slurped template can be edited by non-programmers, so for safety we 'use Safe'. Here is the code so far:
my $code; my $compartment = new Safe; $compartment->share('$href'); $code = $compartment->reval($$file); if ($@) { _sAbort( $r, "Restricted eval failed! Unable to compile format of +type $type:", $@ ); return DONE; }
The real problem now comes up. ANY use/call of $code->($href) will activate the print statements inside the slurped file. This was fine with the following:
$r->send_http_header('text/html'); $code->($href); return DONE;
But now I am trying to instead grab the output of $code and stick it as text into an email. (You guessed it - I'm trying to email generated web pages - HTML email, yuck!).

Surfing through the various books, I find no simple way to redirect STDOUT to a variable for capture. The following code failed miserably:
my $open_err; my $data; open(OUTPUT, "$code->($href) |") or $open_err = $!; if ($open_err) { _sAbort($r,"Cannot open a pipe",$!); return DONE; } local $/ = undef; $data = <OUTPUT>; close(OUTPUT);
Any of you geniouses know offhand how I can get this darned HTML output from the code ref $code into a simple variable so i can give it to MIME::Lite as data? Much gracious bowing and licking of boots are offered to the one who can solve this for me!

What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

In reply to Redirecting STDOUT by tame1

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.