matt.schnarr has asked for the wisdom of the Perl Monks concerning the following question:

I am using a thread to open a file handle, and would like to pass control of that file handle to a different thread. Unfortunatley it doesn't seem like I can close a file handle in a different thread then which it was opened.

I'm using Perl 5.8.0 and have now switched to ithreads.

Does anyone have any ideas?

#!/usr/bin/perl use threads; $filename = "/tmp/input.log"; $outputdir = "/var/log/"; $first_event = 1; $position = 0; $time = time(); # Define Timer sub timer { my $counter = 0; while (1) { sleep(1); if ($counter >= 60) { $counter = 0; # #I can't seem to close FILE from the main thre +ad... why? # close (FILE); $outputfile = $outputdir . "log-". time(); open(FILE, "> $outputfile") || die("Can't open + $outputfile to write to"); } $counter++; } } $thr1 = threads->new(\&timer); # Spawn the thread open(TAIL, "tail --follow=name $filename|"); $outputfile = $outputdir . "log-". time(); open(FILE, "> $outputfile") || die("Can't open $outputfile to write to +"); while (<TAIL>) { # do some stuff and print to FILE }

Thanks!

Matthew Schnarr

Considered: bronto Delete: Isn't this a duplicate of id://387514 thread?
Unconsidered: ysth enough keep votes - Keep/Edit/Delete: 6/4/27