Re: Need dependency-free way of capturing verbose function output in a variable

[email protected] (James E Keenan)
Newsgroups perl.cpan.workers
Message-ID <[email protected]>
On 07/19/2015 07:00 AM, Zefram wrote:
> James E Keenan wrote:
>>                      That leaves only the problem of capturing STDOUT
>
>      close(STDOUT);
>      open(STDOUT, ">", \$captured_stdout);
>
> -zefram
>

Thanks for the suggestions.  I had looked at 'perldoc -f select' but 
couldn't quite figure out the sequence of commands.  This got me much 
closer.  Here's what I currently have, which is DWIMming:

sub _run_for_verbose {
     my $coderef = shift;
     my $stdout;
     my $oldfh = select(STDOUT);
     local $| = 1;
     close STDOUT;
     open STDOUT, '>', \$stdout;
     &$coderef;
     close STDOUT;
     return $stdout;
}

Called like:

     is(_run_for_verbose(sub {@created = mkpath($dir, 1)}),
         "mkdir $base\nmkdir $dir\n",
         'mkpath verbose (old style 1)'
     );

Thank you very much.
Jim Keenan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.