in reply to Could I redirect stdout back into my script?
#!/usr/bin/perl -w use strict; use File::Find; use File::Path; use File::Slurp; use Pod::Select; use IO::Scalar; use Benchmark; my $dir = "c:/perl/lib/Pod"; my %podstrings; timethese(10, { 'Tmpfile' => sub { %podstrings = (); findTmpfile(); # printf "%d\n", scalar keys %podstrings; }, 'TieStdout' => sub { %podstrings = (); findTieStdout(); # printf "%d\n", scalar keys %podstrings; }, }); sub findTmpfile { find ( sub { return if -d; my $tmpfile = "$dir/tmpfile.txt"; return if $File::Find::name eq $tmpfile; podselect {-output => $tmpfile}, $File::Find::name; if (-s $tmpfile) { $podstrings{$File::Find::name} = scalar read_file($tmpfile +); } unlink $tmpfile if -f $tmpfile; }, $dir); } sub findTieStdout { find ( sub { return if -d; my $output_string = ''; tie *STDOUT, 'IO::Scalar', \$output_string; podselect $File::Find::name; untie *STDOUT; if ($output_string) { $podstrings{$File::Find::name} = $output_string; } }, $dir); } __END__ Benchmark: timing 10 iterations of Tmpfile, findTieStdout... Tmpfile: 123 wallclock secs (120.08 usr + 2.71 sys = 122.80 CPU) @ + 0.08/s (n=10) TieStdout: 103 wallclock secs (102.45 usr + 0.82 sys = 103.27 CPU) @ + 0.10/s (n=10)
|
|---|