Re: Need dependency-free way of capturing verbose function output in a variable
[email protected] (Aristotle Pagaltzis)
| Newsgroups | perl.cpan.workers |
|---|---|
| Message-ID | <[email protected]> |
* James E Keenan <[email protected]> [2015-07-19 01:25]: > 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. perldoc -f select # ? > 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; open my $strfh, '>', \ > my $captured_verbose_output; my $prevfh = select $strfh; > &$code; select $prevfh; > return $captured_verbose_output; > } > > Suggestions? Thanks in advance. HTH -- *AUTOLOAD=*_;sub _{s/..([^:]*)$/()[print$1,(",$\/"," ")[defined wantarray]]/e;$_} &Just->another->Perl->hack; #Aristotle Pagaltzis // <http://plasmasturm.org/>