Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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>

In reply to Re: Variable triggers global destruction hang by ikegami
in thread Variable triggers global destruction hang by saintmike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found