Bug#706749: perldoc: Minor bug in fileno example

Niels Thykier niels at thykier.net
Sat May 4 09:07:16 UTC 2013


Package: perl
Version: 5.14.2-21
Severity: minor

perldoc -f fileno

"""
fileno FILEHANDLE

[...]  If there is no real file descriptor at the OS level, as can
happen with filehandles connected to memory objects via "open" with a
reference for the third argument, -1 is returned.

[...]

You can use this to find out whether two handles refer to the same underlying descriptor:

    if (fileno(THIS) == fileno(THAT)) {
        print "THIS and THAT are dups\n";
    }
"""

This can be demonstrated by the following little snippet:

"""
#!/usr/bin/perl
use strict;
use warnings;
use autodie;

my $data1 = 'Hallo';
my $data2 = 'World';

open(FH1, '<', \$data1);
open(FH2, '<', \$data2);

if (fileno(FH1) == fileno(FH2)) {
    print "Has \"same\" fileno but obviously not dups\n";
}

print <FH1>, ' ', <FH2>, "\n";

close(FH1);
close(FH2);
"""

I believe a more accurate example (although a bit more verbose) would
be:

"""
    if (fileno(THIS) != -1 && fileno(THIS) == fileno(THAT)) {
        print "THIS and THAT are dups\n";
    } elsif (fileno(THIS) != -1 && fileno(THAT) != -1) {
        print "THIS and THAT have different underlying file descriptors\n";
    } else {
        print "At least one of THIS and THAT does not have a real file descriptor\n";
    }
"""

~Niels




More information about the Perl-maintainers mailing list