+3  A: 

Use kill. If you set a variable in the parent before your fork, you don't need any external options.

my $parent_pid = $$; # Keep a reference to the parent

my $pid = fork();
if ($pid) {
    print "Im going to wait (Im the parent); 
    my child is: $pid. The part Im going to use is: $partId \n";
    push(@childs, $pid);
} 
elsif ($pid == 0) {
   my $slp = 5 * $_;
   print "$_ : Im going to execute my code (Im a child) and Im going to wait like $slp seconds. The part Im going to use is: $partId\n";
   sleep $slp;
   print "$_ : I finished my sleep\n";

   kill 20, $parent_pid # Send a signal to the parent, 20 is SIGCHLD

   exit($slp);
} 

See perldoc -f kill for more details on the kill call Another option if you need to do more complex things is to use POE

Bruce Armstrong
now Im using it! thanks!
pablo89
+2  A: 

Forks::Super has a good interface for passing messages between parent and child processes (interprocess communication). With this interface, you can pass messages to the child's STDIN and read from the child's STDOUT and STDERR handles.

use Forks::Super;

# set up channels to child's STDIN/STDOUT/STDERR with blocking I/O
my $pid = fork { child_fh => 'all,block' };

if ($pid) { # parent
    $pid->write_stdin("Hello world\n");
    my $msg_from_child = $pid->read_stdout(); # <-- "HELLO WORLD\n"
    print "Message from child to parent: $msg_from_child";
} 
elsif (defined($pid) && $pid == 0) { # child
    sleep 1;
    my $msg_from_parent = <STDIN>;            # <-- "Hello world\n"
    my $output = uc $msg_from_parent;
    print $output;
    exit 0;
} 
else{
    die "couldn’t fork: $!\n";
}   
socket puppet