tutorial/pseudothread-fork.pl
use strict;
use warnings;
use File::Spec;
my $resultfile = File::Spec->catdir($ARGV[0], $ARGV[1]);
# in our method of tracking the thread completion, we must be able to write to the out file
# but it cannot exist before hand, since it is deleted after it is read
if (-d $ARGV[0] && -w $ARGV[0] && !-e $resultfile) {
# standard fork behavior to follow!
my $pid = fork;
# here is where the "thread" goes, with all forks, the $pid at this point is set to 0
if ($pid == 0) {
# the sleep is just an example to show the non blocking nature of this method
sleep (3);
# Attempt to open for writing, it is -w, so it SHOULD, but can't be to safe0
if (open (RESULT, '>', $resultfile)) {
print RESULT (defined $ARGV[2] ? $ARGV[2] : "George!");
close RESULT; # close that file handle!
}
}
}
# all done, either the parent will continue at the point of the system call, or the fork process will just terminate