in reply to Re^3: Getting a Perl program to close a command it starts when the perl program exits?
in thread Getting a Perl program to close a command it starts when the perl program exits?
The problem is the order in which the handle's destructor and the END block are executed. If the file handle is destroyed first, you have a problem. When destroying a file handle, perl closes it, and closing a file handle opened with open '...|' causes perl to wait until the child process exits. Seeing as the kill command hasn't been executed, that won't happen.
OK, I think I understand now.
The difference is that when you open the lexical, there's an (IO::Handle) object implicitly created. That object gets a call to DESTROY before the END block is called. The DESTROY method calls close, which has to wait for the subprocess to finish, which it never does.
In the case of a global file handle, Perl itself will close it after the END block is called. In that case, END can kill the process and everything else goes fine.
package Foo; sub new { bless {} } sub DESTROY { print "DESTROY()\n" } package main; my $foo = Foo->new(); print "I have a foo.\n"; END { print "END()\n" } exit; __END__ I have a foo. DESTROY() END()
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Getting a Perl program to close a command it starts when the perl program exits?
by ikegami (Patriarch) on Aug 29, 2007 at 19:00 UTC |