in reply to Forking sub inside a module

The entire process will be forked, but both forks will continue running from the point the process forked. And since the data is also forked (copied), there's no problem. You'll get something like:
foreach my $node_name (@nodes) { my $port_hashref = Mod1->sub1($node_name); my $pid = fork; die "Unable to fork: $!\n" unless defined $pid; unless ($pid) { # Child process. Mod2->sub2($node_name, $port_hashref); exit; # When done, child should terminate. } } 1 while -1 != wait; # Reap children.

Replies are listed 'Best First'.
Re^2: Forking sub inside a module
by vbruno (Novice) on Jun 15, 2009 at 16:18 UTC
    Oh, that isn't so hard! I was thinking the forked process would start over at the beginning.

    I am not clear on the last line. I understand that I will want to wait for the last child to finish before exiting the parent, but is there a less obfuscated way of saying that?
      There's nothing obfuscated there. Perhaps the 1 in void context is confusing you? It can be avoided as follows:
      while (-1 != wait) {}
        Maybe it's just me, but I find
        while (wait != -1) {}
        more natural than
        while (-1 != wait) {}
        since 'wait' is what I want to check :o)

        -- Time flies when you don't know what you're doing