Thread

Posted on Tue Jul 25 13:13:21 2006 by jmaier
Implement UNTIE ?
Hi,

I am going to use Tie::Handle::ToMemory to collect a program's output via STDOUT into a scalar. As such, I have a package that takes care of saving STDOUT, replacing it with a tied handle and, later, postprocessing the collected output and restoring the original STDOUT. As such, this package will at one time call untie *STDOUT; in order to get rid of the tie, but this yields a warning about 1 inner reference that still exists.

Now I am not entirely sure what this warning actually points to, but even a small test script that undefs just about anything it can, except *STDOUT - of course, since that's what I want to untie -, still throws the same warning.

After doing some research (read: wild Googling), I learned that this warning is being suppressed if the tied package defines an UNTIE sub and, io and behold, saying BEGIN { sub Tie::Handle::ToMemory::UNTIE {} } does in fact get rid of the warning - but I'm a little curious about that.

Is there a good reason why Tie::Handle::ToMemory does not define UNTIE itself?
And if so, is there a good reason that I'm not aware of why shoving UNTIE down its throat is a Bad Idea™?
And finally, if there is such a reason, is there a way other than { no warnings qw(all); untie *STDOUT; } to avoid the warning?

Cheers,
-- Jens
Direct Responses: 2691 | Write a response
Posted on Tue Jul 25 13:28:24 2006 by dagolden in response to 2690
Re: Implement UNTIE ?

Well, the honest answer is that I didn't implement UNTIE because I wrote this long enough ago that I didn't realize I needed to! I wrote Tie::Handle::ToMemory for a very specific purpose where I needed/wanted a FIFO queue.

For your purpose, collecting output from a program, I suggest you consider several alternatives. Personally, I use IPC::Run3 for that kind of thing. It's very portable and works like this:

use IPC::Run3; my ($out, $err); my @cmd = qw/program -abc input.txt/; # or whatever run3 \@cmd, undef, \$out, \$err; # $out and $err will have the output of the run

Regards, David

Write a response