Ltf4an
Ltf4an

Reputation: 837

What is a built-in name for a filehandle in Perl?

None of the names listed by perl -f ref seems to be for a filehandle. The following code returns GLOB, so it seems that filehandle is internally mapped to or managed as a typeglob. Is that correct?

open FH, '>', "out.txt";
my $ref = \*FH;
print "ref \$ref:", ref $ref, "\n";

Does a filehandle have its own type name?

Upvotes: 1

Views: 177

Answers (1)

ikegami
ikegami

Reputation: 385506

Does a filehandle have its own type name?

IO.

$ perl -MDevel::Peek -e'Dump(*STDOUT{IO});'
SV = IV(0x3ba7118) at 0x3ba7128
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x3bc4390
  SV = PVIO(0x3bc8668) at 0x3bc4390
    REFCNT = 3
    FLAGS = (OBJECT)
    STASH = 0x3bc3fa0   "IO::File"
    IFP = 0x3bbe180
    OFP = 0x3bbe180
    DIRP = 0x0
    LINES = 0
    PAGE = 0
    PAGE_LEN = 60
    LINES_LEFT = 0
    TOP_GV = 0x0
    FMT_GV = 0x0
    BOTTOM_GV = 0x0
    TYPE = '>'
    FLAGS = 0x0

Technically, an IO object can have three handles associated with it: An input file handle (IFP), an output file handle (OFP), and a directory handle (e.g. created using opendir, DIRP). Usually, only one of the input and output handles are set, or they are both set to the same handle. Doing open(FOO, ...); opendir(FOO, ...); <FOO>; readdir(FOO); would work fine because of the separate file and directory handles.

it seems filehandle is internally mapped to or managed as a typeglob. Is that correct?

Both file and directory IO objects are normally encapsulated in a glob, yes. But it's not required.

$ perl -E'
   my $fh = "STDOUT";     say($fh "$fh");
   my $fh = *STDOUT;      say($fh "$fh");
   my $fh = \*STDOUT;     say($fh "$fh");
   my $fh = *STDOUT{IO};  say($fh "$fh");
'
STDOUT                    # Name
*main::STDOUT             # Glob
GLOB(0x1175a48)           # Reference to a glob. (This is returned by open $fh)
IO::File=IO(0x1175a60)    # Reference to a (blessed) IO.

Upvotes: 9

Related Questions