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]>
As part of the work which Richard Elberger and I are doing for 
File-Path, we would like to reduce the test suite's dependency on 
libraries not distributed with the Perl 5 core.  Specifically, that 
means eliminating use of Test::Output functions stdout_is(), stderr_is() 
and stderr_like().

I know how to use $SIG{__WARN__} and $SIG{__DIE__} to replace the last 
two functions.  That leaves only the problem of capturing STDOUT when a 
user has requested verbose output from mkpath, make_path, rmtree or 
remove_tree.

If we were not working on a library distributed with core, I would use 
any one of IO::Capture, IO::CaptureOutput or Capture::Tiny.  (I've even 
contributed to some of them!)  But even Capture::Tiny (a) is not (yet) 
core; and (b) has a dependency on File::Temp, which in turn has a 
dependency on File::Path.

ISTR that there's some magic you can perform with 'select' to obtain 
this objective, but I can't seem to locate what I have in mind on the 
network.  What I want is something that, with just core Perl will be 
callable like this:

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

... where _run_for_verbose() has this look:

     sub _run_for_verbose {
         my $code = shift;
         my $captured_verbose_output;
         ... # filehandle magic; no non-core libraries
         &$code;
         ... # complete the magic
         return $captured_verbose_output;
     }

Suggestions?  Thanks in advance.

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.