Then you should definitely use a module, such as one of those recommended by aitap above:
#! perl
use strict;
use warnings;
use autodie;
use Capture::Tiny qw( tee_stdout );
my %test_hash = ( Foo => 1, Bar => 1, Baz => 1, Nix => 0 );
foreach (keys %test_hash)
{
if ($test_hash{$_} == 1)
{
my $logfile = $_ . '_log.txt';
print "the logfile is $logfile\n";
require "$_.pm";
my ($stdout, @result) = tee_stdout { $_->$_() };
open(my $out, '>', $logfile);
print $out $stdout;
close($out);
}
}
++aitap for recommending Capture::Tiny! I already had it on my system (it came with Strawberry Perl), but I hadn’t come across it before. Brilliant!
Athanasius <°(((>< contra mundum
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|