in reply to In a perl sub, how do I capture STDOUT, STDERR that comes from another perl sub?

You can localize STDOUT and STDERR this way:
sub printInformationWrapper { open (local *STDOUT,'>',\(my $var)); thing that prints to STDOUT return $var; }
This method sometimes has problems with printing Unicode. (unless you use open ':utf8'; use encoding 'utf8';, which is the right way to go for unicode anyway).

This is supposed to work without the pragmata (http://community.livejournal.com/ru_perl/183306.html):

{ open (local *STDOUT,'>:utf8',\(my $b="\x{FEFF}")); printing_stuff return $b; }
though I've not tested it.

IO::String is probably a less magical way to tie a filehandle to a string. Then you localize STDOUT like above, assigning your filehandle.

Replies are listed 'Best First'.
Re^2: n a perl sub, how do I capture STDOUT, STDERR that comes from another perl sub?
by blazar (Canon) on Aug 18, 2007 at 09:57 UTC
    open (local *STDOUT,'>:utf8',\(my $b="\x{FEFF}"));

    I was bitten in the neck by this, at least with :raw. I don't know if with :utf8 it's the same. Of course a separate binmode is a viable workaround. Yes, I'd like to do that too in the open to start with.