Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How can I read DATA in parent and child?

by Skeeve (Parson)
on Feb 18, 2019 at 14:17 UTC ( #1230106=note: print w/replies, xml ) Need Help??


in reply to How can I read DATA in parent and child?

So I just noticed: It seems to have to do with buffering.

When I appen a lot of lines containing some "x" I received response from parent and child:

$ perl x Start p p: a Start c c: x c: xxxxxxxxxx p: b c: xxxxxxxxxx p: c c: xxxxxxxxxx p: d c: xxxxxxxxxx p: e c: xxxxxxxxxx p: xxxxxxxxxx

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^2: How can I read DATA in parent and child?
by Skeeve (Parson) on Feb 18, 2019 at 14:24 UTC

    This works:

    use IO::Handle; use strict; use warnings; my $x= IO::Handle->new(); $x->fdopen(fileno(DATA), "r"); my $y= IO::Handle->new(); $y->fdopen(fileno(DATA), "r"); my $pos= tell *DATA; my $pid= fork(); die unless defined $pid; if ($pid == 0) { seek $x, $pos, 0; print "Start c\n"; while (<$x>) { print "c: $_"; sleep 1; } print "stop c\n"; } else { seek $y, $pos, 0; print "Start p\n"; while (<$y>) { print "p: $_"; sleep 1; } print "stop p\n"; } __DATA__ a b c d e

    Simply reposition the filepointer…


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

      The docs on fork perlfork say that parent and child share the same seek pointer:

      Any filehandles open at the time of the fork() will be dup()-ed. Thus, the files can be closed independently in the parent and child, but beware that the dup()-ed handles will still share the same seek pointer. Changing the seek position in the parent will change it in the child and vice-versa. One can avoid this by opening files that need distinct seek pointers separately in the child.

      So I find it surprising that this version works.

        > So I find it surprising that this version works.

        It works sometimes . I've run it 10 times and got the following output twice:

        Start p p: a Start c stop c p: b p: c p: d p: e stop p

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2020-09-20 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I don’t succeed, I …










    Results (120 votes). Check out past polls.

    Notices?