http://qs321.pair.com?node_id=617151

Tommy has asked for the wisdom of the Perl Monks concerning the following question:

tommy@blessed ~/
$ perldoc -f fileno

fileno FILEHANDLE
Returns the file descriptor for a filehandle, or
undefined if the filehandle is not open.
...
(Filehandles connected to memory objects via new
features of "open" may return undefined even though
they are open.)

...So I am really wondering if some perlio guru out there could elaborate on that last statement of the perl documentation for fileno(). What, pray tell, is it talking about?

While I am doing my own research, I still appreciate the insight my monkly bretheren may have to offer. Many thanks for the insight.

--
Tommy

Replies are listed 'Best First'.
Re: perldoc -f fileno
by imp (Priest) on May 24, 2007 at 03:58 UTC
    You can open a filehandle that uses a scalar reference as the data source, like this:
    use strict; use warnings; my $data = "a\nb\n"; open my $fh, '<', \$data; my $fileno = fileno $fh; printf "fileno: %s\n", defined $fileno ? $fileno : '(undef)'; print for <$fh>; close $fh; $fileno = fileno $fh; printf "fileno: %s\n", defined $fileno ? $fileno : '(undef)';
Re: perldoc -f fileno
by sgifford (Prior) on May 24, 2007 at 16:27 UTC
    Somewhat more generally, fileno returns the operating system's file descriptor number for the open file. Perl will sometimes let you pretend something is a file which really isn't, as in imp's example. In that case, Perl will give you a filehandle that isn't connected to a real file. If you create such a filehandle then ask for the operating system's descriptor, there isn't really an open file, so undef is returned.