tags:

views:

50

answers:

1

The perldoc for fileno says

Filehandles connected to memory objects via new features of open may return undefined even though they are open.

Is this referring to new style calls to open in general, or to the IO::Scalar style calls to open?

+4  A: 

This code seems to say that it applies only to the IO::Scalar version of open. This is probably because there is no underlying OS level fileno associated with the filehandle.

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

my $fakefile = "foo\nbar\nbaz\n";
open my $fake, "<", \$fakefile
    or die "could not open fakefile [$fakefile]: $!";

open my $script, "<", $0
    or die "could not open self for reading: $!";

print "fake: ", my_fileno($fakefile), "\nreal: ", my_fileno($script), "\n";

sub my_fileno {
    my $fileno = fileno shift;
    $fileno //= "undef";
    return $fileno;
}
Chas. Owens
I guess it applies only to IO::Scalar as you have stated.
Alan Haggai Alavi
Right, but that is only a guess. I am hoping someone who actually knows for certain will answer or comment.
Chas. Owens
I think the key is "memory objects", but perldoc -f open doesn't mention "memory objects". It does mention '"in memory" files' which may or may not be the same thing. I smell a documentation patch.
Chas. Owens
I agree that "memory objects" is the key. That interpretation is consistent with "new features of open" as well. I can't see how fileno would return anything but undef for in memory files. You might have better luck asking this on comp.lang.perl.misc or comp.lang.perl.moderated as some of the people who hang out there have knowledge of the internals. I've used those groups as a resource for ferreting out details before submitting doc patches before.
Michael Carman