S
Sako
I am trying to teach myself perl by writing a program I've been meaning
to implement, so I am pretty green in perl. I'm having problems sharing
filehandles opened by a thread - been RTFM for a few days, but am having
no luck.
I am attempting to write a threaded server program that listens on a
socket for requests, then passes the socket's filehandle to an event
processor routine, while the listener thread keeps on listening.
However, I cannot seem to be able to successfully pass the filehandle
from the listener thread to the event handler.
For testing purposes, I've tried to get the logic down using a thread to
open a filehandle and pass it back to the non-threaded routine as follows:
--------- start ------------
#!/usr/bin/perl
use threads;
use threads::shared;
my $FH : shared;
threads->new(sub {
open($LFH,"< /tmp/junk") || die $!;
$FH=$LFH;
print "[$FH]\n";
#while (<$LFH>) {print "> $_";}
})->join;
print "[$FH]\n";
while (<$FH>) {print "> $_";}
---------- end -------------
Running it gives:
"thread failed to start: Invalid value for shared scalar at ./x.pl line 10."
Any help on how to share a filehandle opened in a thread would be
GREATLY appreciated.
TIA
to implement, so I am pretty green in perl. I'm having problems sharing
filehandles opened by a thread - been RTFM for a few days, but am having
no luck.
I am attempting to write a threaded server program that listens on a
socket for requests, then passes the socket's filehandle to an event
processor routine, while the listener thread keeps on listening.
However, I cannot seem to be able to successfully pass the filehandle
from the listener thread to the event handler.
For testing purposes, I've tried to get the logic down using a thread to
open a filehandle and pass it back to the non-threaded routine as follows:
--------- start ------------
#!/usr/bin/perl
use threads;
use threads::shared;
my $FH : shared;
threads->new(sub {
open($LFH,"< /tmp/junk") || die $!;
$FH=$LFH;
print "[$FH]\n";
#while (<$LFH>) {print "> $_";}
})->join;
print "[$FH]\n";
while (<$FH>) {print "> $_";}
---------- end -------------
Running it gives:
"thread failed to start: Invalid value for shared scalar at ./x.pl line 10."
Any help on how to share a filehandle opened in a thread would be
GREATLY appreciated.
TIA