When a process forks the child gets a copy of the parent's environment, but it's not the same set so adding to the arrays in the child won't affect those in the parent. This is more a task for threading rather than fork'ing, or write your output to a file (With suitable file locking) and when done have the parent read in from there.

Forks are for where you want the child to spin off with minimal contact, threads are where you want shared data and so forth. Personally I tend to stick to fork, this way I don't have to deal with threadsafe code, and use the file technique above.

Another small point, you call the 'next' from the child. This is, I believe, very bad since you don't end up calling $pm->finish which is what I'd replace your next OUTER calls with.

As an added freebie, here's the routine I'm currently using to do the 'adding to a file' bit. I appreciate this isn't particularly wonderous code, but it does seem to work.

# Nice little code fragment to append to the end of a file. # Takes the name of the file to append to, followed by # all of the lines of text to append. sub SafeAppend { my ($file, @content) = @_; s/\n*$/\n/ foreach @content; local *FILE; open FILE, ">>$file" or die "ERROR: Cannot write $file: $!\n"; flock (FILE, LOCK_EX) or die "ERROR: Cannot lock $file: $!\n"; seek (FILE,0,2) or die "ERROR: Cannot seek to end of $file: $!\n"; print FILE foreach @content; close FILE or die "ERROR: Cannot close $file: $!\n"; }

In reply to Re: Fork Me I need help by Molt
in thread Fork Me I need help by neilwatson

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.