use strict;
use warnings;
use 5.010;
my $child_pid = fork;
$| = 1;
if (!defined $child_pid) {
say "**My error**: couldn't fork: $!";
}
if ($child_pid) { #then in parent
say "in parent...";
sleep 1;
}
else { #then in child
open my $OUTFILE, '>', 'data1.txt'
or die "**My error: couldn't open data1.txt: $!";
for (1 .. 10) {
say $OUTFILE $_;
sleep 2;
}
close $OUTFILE;
}
####
$ perl 1perl.pl
in parent...
$ cat data1.txt
$
####
$ perl 1perl.pl
in parent...
$ cat data1.txt
1
2
3
4
5
6
7
8
9
10
$