in reply to Variable triggers global destruction hang
I don't get any difference in behaviour for your code. What Perl version and system?
Now, I do expect the following difference in behaviour:
$ perl -e'open my $fh, "cat |" or die $!' <waits forever> $ perl -e'open our $fh, "cat |" or die $!' <returns immediately>
When all references to a variable cease to exist, it is freed. In the case of this kind of file handle, Perl wait for the child process to end. cat never ends unless told to, so perl can wait for a long time.
In the first case, all references to $fh cease to exist when execution reaches the end of the file. Thus, when Perl reaches that point of the program, it starts waiting for cat to end.
In the second case, the file handle survives beyond the end of the file and into global destruction since it exists in the symbol table. The "bug" is that file handles aren't closed during global destruction — Perl let's the system do it — so cat is left running, ignored.
I suppose you could call it a bug. It's definitely known behaviour. It's easy to work around, though. Just call close($fh); explicitly.
$ perl -e'open our $fh, "cat |" or die $!; close($fh)' <waits forever>
On the flip side, you can always force cat to terminate early using kill.
$ perl -e'my $pid = open my $fh, "cat |" or die $!; kill TERM => $pid' <returns immediately>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Variable triggers global destruction hang
by saintmike (Vicar) on Sep 12, 2009 at 06:26 UTC | |
by ikegami (Patriarch) on Sep 12, 2009 at 06:44 UTC | |
by ig (Vicar) on Sep 12, 2009 at 07:03 UTC | |
by ikegami (Patriarch) on Sep 12, 2009 at 07:11 UTC | |
by ig (Vicar) on Sep 12, 2009 at 07:25 UTC | |
| |
by ig (Vicar) on Sep 12, 2009 at 07:19 UTC |