Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The following mundane piece of code does piped opens to 3 external processes that will each run for 3 seconds, and then times them out after 2, 3 & 4 seconds respectively. It gathers whatever output they produce before being timed out and (attempts) to return it to the caller.

UPDATE: Code and trace below updated to reflect andal's post below, and the information that error 1 below is fixed in 5.16. Error 2, the "modification of readonly value" remains; though in a slightly changed form as reflected by the new trace.

See the console trace after the __END__ token for the results::

#! perl -slw use strict; use threads stack_size => 4096; use threads::shared; sub timedCommand { my( $timeout, $cmd, @args ) = @_; my $pid :shared; my @input :shared; async { # local $_; ### LINE C ### $pid = open my $fh, "$cmd @args |" or die "$!, $^E"; push @input, $_ while <$fh>; }->detach; sleep 1 until $pid; sleep 1 while kill 0, $pid and $timeout--; kill 3, $pid if $timeout; print "@input"; ### LINE A ### return @input; } my $cmd = q[ perl -E"$|++; sleep(1), say for 1 .. $ARGV[0]" ]; print timedCommand( 3, $cmd, $_ ) for 2, 3, 4; ### LINE B ### __END__ C:\test>\perl64-16\bin\perl.exe timedCommand.pl Thread 1 terminated abnormally: Modification of a read-only value atte +mpted at timedCommand.pl line 15. Thread 2 terminated abnormally: Modification of a read-only value atte +mpted at timedCommand.pl line 15.

The two mysteries are:

  1. Where does the contents of @input disappear to between LINE A and LINE B?

    Update: This part is fixed in 5.16.

  2. Why, if I comment out LINE C, do I get:
    C:\test>timedCommand.pl Thread 1 terminated abnormally: Modification of a read-only value atte +mpted at C:\test\timedCommand.pl line 15. Thread 2 terminated abnormally: Modification of a read-only value atte +mpted at C:\test\timedCommand.pl line 15. Thread 3 terminated abnormally: Modification of a read-only value atte +mpted at C:\test\timedCommand.pl line 15.

    UPDATE: This problem remains, though with slightly different output. Only two copies of teh error message are produced.

Perl bugs or bad code?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


In reply to One piece of code, two mysteries! (UPDATED.) by BrowserUk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2014-09-21 07:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (167 votes), past polls