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

To put the short story long, I have this app which I can use via Win32::OLE. It's "mildly" documented and supported. I'm on my own.

There come with it some Win32::OLE=HASH objects. Each of them can do $obj->print. It prints some content directly to screen (i.e. STDOUT). If only it returned the content as string or something, such that I could use it, say, to print it to browser.

I can't tell if there're ways to do that with those objects. Although I can "dump" the objects and get the raw data I need, they're not formatted and can comprise of complicated data structure. I don't want to reinvent the print methods for them all. Therefore, I thought, if I could capture the content of $obj->print, it would be good.

So, can I possibly redirect STDOUT to a scalar variable, without using filehandle and writing it to a file first? Or should I deal with the issue differently?

Thanks.
  • Comment on Redirect STDOUT to Scalar W/o Filehandle?

Replies are listed 'Best First'.
Re: Redirect STDOUT to Scalar W/o Filehandle?
by sauoq (Abbot) on Jun 19, 2003 at 03:53 UTC
    use IO::Scalar; $tied_stdout = tie *STDOUT, 'IO::Scalar'; print 'this was printed into $tied_stdout'; print STDERR "And to prove it: ", $tied_stdout, "\n";
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Redirect STDOUT to Scalar W/o Filehandle?
by Zaxo (Archbishop) on Jun 19, 2003 at 07:45 UTC

    In perl 5.8, open a referefence to the string in the filename slot:

    my $string; { open local(STDOUT), '>', \$string; $obj->print; }

    After Compline,
    Zaxo