Subject: | closing STDOUT/ERR in detach redirects warn() |
detach() closes STDOUT, STDERR rather than redirecting them to /dev/null
As a result, the following code snippet has a rather surprising effect:
--------------
use strict;
use warnings;
use App::Daemon qw/daemonize/;
daemonize();
use Test::Fork;
use IO::Socket;
fork_ok(
1,
sub {
my $sock = new IO::Socket::INET (
LocalHost => 'localhost',
LocalPort => 9001,
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
open LOG,">/tmp/weirdlog";
ok ($sock, "Created listener ok");
my $accept = $sock->accept();
while (<$accept>) {
print LOG ">>>2: $_";
}
}
);
sleep 1;
my $sock = IO::Socket::INET->new(
PeerHost => 'localhost',
PeerPort => 9001,
Proto => 'TCP',
);
my $d;
my $data = "foo: $d\n"; # this will generate warning "Use of uninitialized value $d..."
print $sock $data;
-------
The warning from the unitialized variable ends up being sent down the socket!