|Problems? Is your data what you think it is?|
Re: Redirecting STDOUT from internal function with 5.6.1 restrictionsby tmoertel (Chaplain)
|on Oct 12, 2004 at 21:59 UTC||Need Help??|
(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.
With this code in place, capturing output from any filehandle becomes easy:
The output is what you would expect (tested on Perl v5.6.1 built for i386-linux):
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.