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

I wish to use the Pod::Html module, but I need the output in a variable, not in a disk file or sent to STDOUT. I've tried the following code,

use IO::Scalar; use Pod::Html; my $temp; tie *STDOUT, 'IO::Scalar', \$temp; pod2html("--infile=$file"); untie *STDOUT;

but the output still goes to the 'real' STDOUT and not to $temp.

Any ideas?

Replies are listed 'Best First'.
Re: Capturing output from Pod::Html to a scalar
by stefp (Vicar) on Sep 07, 2001 at 20:09 UTC
    if you dig in Pod::HTML, you will see that it opens another handle to access STDOUT behind your back and that breaks you tie. So you are probably better of to do:
    my $temp = `pod2html $file`;
    Relevant extract from Pod::HTML
    $htmlfile = $opt_outfile if defined $opt_outfile; ... $htmlfile = "-" unless $htmlfile; # stdout ... # open the output file print STDERR ">$htmlfile"; open(HTML, ">$htmlfile") || die "$0: cannot open $htmlfile file for output: $!\n";

    -- stefp

(jeffa) Re: Capturing output from Pod::Html to a scalar
by jeffa (Bishop) on Sep 07, 2001 at 19:59 UTC
    Ideas? yes.
    A good answer? no

    The problem here is how to tie STDOUT to a variable, and i really don't know how to do that. However, you can always use 2 scripts to get the output of pod2html() into a variable, via backticks, opening a pipe, etc.

    Don't forget, there is also pod2html, which comes with the standard dist (on *nix at least).

    Now, if anybody knows how to capture STDOUT into a variable via tie, please let me know. ;)

    jeffa

      I originally thought about using backticks, however pod2html is in a different location on my home PC to the UNIX box at work so this would not be portable.

      If all else fails, I'll use a temporary file but I'm sure there's a better way.

      Many thanks,

      JJ

Re: Capturing output from Pod::Html to a scalar
by melguin (Pilgrim) on Sep 07, 2001 at 20:00 UTC
    It seem that what you are trying to do is redirect STDOUT to a file/variable, then change it back again. Check out the node redirecting STDERR, then back again. It deals with the same problem.

    The last post on that node is what worked from me. It's pretty much straight from perlfunc:open.

    melguin.