atistler has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to forking and do not really understand it too well. I basically want to create a buffer for my socket and read and write from that buffer. So I will push all incoming stuff from my socket to an array, and at the same time be reading stuff out from the array (shifting the first element). I wanted one process for reading and one for writing. Cannot figure it out. Any help would be most welcome. Thanks,
#!/usr/bin/perl use IO::Socket::INET; my @array; my $sock = IO::Socket::INET->new( LocalAddr => 'loghost', LocalPort => 514, Proto => 'udp') or die "Cant establish socket"; die "Fork failed: Damn it!!!\n" unless defined($kid = fork); if ($kid) { while (<$sock>) { print "SOCKIN: $_\n"; push ( @array, $_ ); } } else { while (@array)) { my $firstelem = shift (@array); print "$firstelem\n"; } }

Replies are listed 'Best First'.
Re: Forking to read and write to an array
by Zaxo (Archbishop) on Jun 23, 2006 at 03:17 UTC

    Once you fork, you have two different processes, with different environments. @array in the child is not the same @array that's in the parent any more.

    See perlipc for treatment of lots of options for talking between processes.

    After Compline,
    Zaxo

Re: Forking to read and write to an array
by NetWallah (Canon) on Jun 23, 2006 at 04:57 UTC
    The push-pop data flow model that you seek, for processing in different run environments (threads) is provided by Thread::Queue, and Thread.

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

Re: Forking to read and write to an array
by sgifford (Prior) on Jun 23, 2006 at 03:45 UTC
    Also, take a look at threads, which do run in the same address space, and can share variables more easily.