I'm working with a Perl script that uses threads and threads::shared. I want to read from a file handle that is opened by a separate thread, but threads::shared can't admit it as a value for a shared scalar.
I'm thinking maybe I can simply share the result of fileno to the other thread and then have it read it. The problem is that I don't know what to do with that number. If the answer is in the documentation, I'm probably not searching for the right thing as I haven't found it yet. How can I get an actual file handle from its fileno value?
If that's not possible, is there a way to open and pass a file handle to another thread after both threads are created?
Thanks in advance for any suggestions.
Use open:
If you specify
'<&=X', whereXis a file descriptor number or a filehandle, then Perl will do an equivalent of C'sfdopenof that file descriptor
my $fileno = 0;
open(my $stdin, "<&=$fileno"); # 2-argument form
open(my $stdin, "<&=", $fileno); # or use 3-argument form
If you prefer an object-oriented approach, you can use IO::File and the fdopen or new_from_fd methods (as Borodin pointed out):
use IO::File;
my $stdin = IO::File->new_from_fd($fileno, 'r');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With