Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Redirecting STDOUT from internal function with 5.6.1 restrictions

by tmoertel (Chaplain)
on Oct 12, 2004 at 21:59 UTC ( #398709=note: print w/ replies, xml ) Need Help??


in reply to Redirecting STDOUT from internal function with 5.6.1 restrictions

(Update: Wrapped the redirecting code into a convenient, reusable function.)

The following code shows one way of doing what you want in 5.6.1, all wrapped into a tidy function. It does use a temporary file, but the file is cleaned up automatically, and File::Temp uses a random file name to protect against certain kinds of file-based attacks.

use File::Temp qw( tempfile ); sub capture_output { my $target_fh = shift; my $temp_fh = tempfile(); my $temp_fd = fileno $temp_fh; local *SAVED; local *TARGET = $target_fh; open SAVED, ">&TARGET" or die "can't remember target"; open TARGET, ">&=$temp_fd" or die "can't redirect target"; my $saved_fh = *SAVED; return sub { seek $temp_fh, 0, 0 or die "can't seek"; # rewind my $captured_output = do { local $/; <$temp_fh> }; close $temp_fh or die "can't close temp file handle"; local (*SAVED, *TARGET) = ($saved_fh, $target_fh); open TARGET, ">&SAVED" or die "can't restore target"; close SAVED or die "can't close SAVED"; return $captured_output; } }
With this code in place, capturing output from any filehandle becomes easy:
print STDERR "before redirection\n"; my $recorder = capture_output(*STDERR); # start print STDERR "during redirection\n"; my $saved_output = $recorder->(); # stop print STDERR "after redirection\n"; print "Saved output = $saved_output";
The output is what you would expect (tested on Perl v5.6.1 built for i386-linux):
before redirection after redirection Saved output = during redirection

Sidebar: I don't know why you think a file-based solution is a hack. Unless you have a really good reason not to use a temp file, you'll probably find it to be the most practical, general-purpose solution.

Because you want to capture what is written to STDERR, you must preserve typical file-descriptor semantics. Using a temp file gives you this behavior for free.

Yes, you could try to use a tied filehandle, but the 5.6.1 perltie docs say that tied-filehandle support is only "partially implemented." More importantly, you'll be hosed if what you're testing ends up writing to the stderr file descriptor directly, bypassing your filehandle. This can easily happen if your code uses external libraries, which just about all code does implicitly via the standard C libraries that Perl links against.

Given that the temp-file solution will work in all situations and has the advantage of being drop-dead simple, my recommendation is to use it. Only if it proves inadequate in actual practice should you worry about more complicated solutions.

Cheers,
Tom


Comment on Re: Redirecting STDOUT from internal function with 5.6.1 restrictions
Select or Download Code
Re^2: Redirecting STDOUT from internal function with 5.6.1 restrictions
by mgc (Novice) on Oct 13, 2004 at 20:44 UTC
    Did I say STDERR? Sorry I meant STDOUT.
    I ended up going with the temp file idea...thanks.
      Did I say STDERR? Sorry I meant STDOUT.

      No need to be sorry. You said STDOUT – rather clearly and several times at that, I see, now that I re-read your orignal post. I'm just a goofball and somehow read it as STDERR. Each and every time. (Note to self: The correct order is: espresso first, post second.)

      Thank you for being gracious about my error and not pointing out how big of a dope I am. Luckily, I didn't hardcode STDERR into my example code, and so I don't look like a complete idiot.

      Anyway, I'm glad that everything worked out, despite mine eyes having deceived me.   :)

      Cheers,
      Tom

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://398709]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-08-28 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls