i'm having some issues with subroutines being ran on a forked
process. the subroutines have print statements which i would like to have
printed to stdout in a format similar to:
"\nthis is my message\n\n"
however, the print format is not as controlled as it is when i do not
fork. sometimes my command line is returned before the final print
statement in the loop. i'll see my first three prints, and then my command
line prompt, with the fourth print statement appearing after the prompt.
do i need a wait() statement? and if so, where? i'm not too familiar with
the function. thanks -c
my code:
#!/usr/bin/perl -w
use strict;
## declare a pid for forking
my $pid;
## loop through devices
for my $node(@devices) {
## if child process exists, move to next device
if ($pid = fork) {
next;
## begin actions within the child process
} elsif(defined($pid)) {
## print a message
&log_action($node);
## explicit exit for child process
exit;
} else {
die "Cant fork for some reason! : $!\n";
## endif
}
## end of for loop
}
sub log_action {
my $i = shift;
print "\nThis message if for node $i.\n\n";
}
Edit by tye to remove tons of trailing spaces from lines