http://paperlined.org/dev/src/pl/tmp/test.pl
#!/usr/bin/perl
# figure out if file handles are shared properly across fork()
#
## AHA! It works!!!! The difference is between buffered- and non-buffered-IO
use strict;
use warnings;
use Data::Dumper;
my $scalar;
# http://search.cpan.org/dist/perl/pod/perlfunc.pod#tell
# There is no systell function. Use sysseek(FH, 0, 1) for that.
#
# More on buffering:
# http://perl.plover.com/FAQs/Buffering.html
#
open FH, "test" or die;
print "[1] tell=" . sysseek(FH,0,1) . "\n";
my $pid = _fork(sub{
sleep 1;
print "[3] tell=" . sysseek(FH,0,1) . "\n";
sleep 2;
print "[5] tell=" . sysseek(FH,0,1) . "\n";
});
my $ret = sysread(FH, $scalar, 5);
print "sysread returned $ret\n\tcontents = $scalar\n";
my $ret = sysread(FH, $scalar, 5);
print "sysread returned $ret\n\tcontents = $scalar\n";
print "[2] tell=" . sysseek(FH,0,1) . "\n";
sleep 2;
sysread(FH, $scalar, 20);
print "[4] tell=" . sysseek(FH,0,1) . "\n";
waitpid($pid, 0);
sub _fork {
my ($child_func) = @_;
defined(my $pid = fork) or die "Unable to fork: $!";
if ($pid == 0) {
$child_func->();
exit;
} else {
return $pid;
}
}
Generated by GNU enscript 1.6.4.