The error occurs because you are calling tempdir before your loop. This makes File::Temp call srand internally, and all subsequent forks inherit the seeded random value.

Thanks for this! I did not know this is how things work, didn't bother looking through the source for more than a few minutes.

FWIW, I call tempdir first because I want all the forks to dump their files into the same dir so I can collect their output easily in the parent when they finish. (I would think this would be a common pattern, but maybe not.)

This is why I included slightly more source than you personally would prefer -- I wanted to show a bit of why I am doing this the way I am doing it. It is reduced considerably from the actual code (down to 26 lines not counting __DATA__ vs. 13 lines for yours). On further consideration, I would probably cut it down as you suggest, and deal with any "why did you do this" questions as they come up (instead of preempting them).

In any case, I doubt I would have stumbled on anything involving srand no matter how much I cut down my code, as it was never in my code! I only know about it because you pointed it out to me, so thanks.

RT


In reply to Re^2: File::Temp randomness when forking by ryantate
in thread File::Temp randomness when forking by ryantate

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.