Sinan Ünür has a way of actually getting the file handle into a variable and is mentioned in the Perl documentation.
However, it involves deep dark Perl mysteries which may be confusing for many developers who aren't familiar with these secrets. You have to know to look in perldata and look in the very last section, and that's if you figure out what Typeglobs are.
There is another way that's less mysterious and easier to understand:
use strict;
use warnings;
use feature qw(say);
use autodie; # Doesn't work. You need to verify
use IO::File;
...
my $file = IO::File->new;
$file->fdopen( fileno( STDIN ), 'r')
or die qq(<STDIN> is not opened.);
while ( my $entry = <$file> ) {
chomp $entry;
say qw(The entry is "$entry");
}
Okay, this isn't exactly crystal clear either, but it does have an advantage that you know where to look for the documentation. fileno
is a function, so that's easy to look up (Returns the file descriptor for a filehandle, or undefined if the filehandle is not open.).
And since fdopen
is a method of IO::File, you know you can find information about that in the IO::File
Perldoc.
Okay, I lied, the documentation for fdopen
is actually in the IO::Handle Perldoc. But, the IO::File
Perldoc does say that it inherits its methods from IO::Handle
. Once you do figure out to look in IO::Handle
, you see an example of this very code right in the synopsis.