http://www.perlmonks.org?node_id=1013274


in reply to Re^2: Close a file and reopen a new one at midnight
in thread Close a file and reopen a new one at midnight

Ok here is how I got it to work. I used a signal handler to close and reopen the file.
while (1) { $SIG{USR1} = sub { close $file; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); my $hrmin = sprintf ("%02d%02d",$hour,$min); my $ymd = sprintf("%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$md +ay,$hour,$min,$sec); my $filename = "/tmp/$ymd.txt"; print $filename, "\n"; open $file, ">$filename" || die("Couldn't open $file"); }; while (my $line = <$sock>) { if ($line =~ m/^(ALARM: )/) {
Now I can just use the log rotate system to send a killall -USR1 to start a new file.