#!/usr/bin/perl use strict; use warnings; my @outer; testSub(); print @outer; sub testSub { my ($pid, $inner); my $cnt = 0; for (1 .. 5) { if (defined($pid = fork)) { if ($pid == 0) { $inner = ++$cnt; push @outer, "Child: $inner\n"; exit(0); } else { print "PID: $pid\n"; next; } } else { die "Can't fork $!" } } }