Ltf4an Ltf4an - 11 days ago 6
Perl Question

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?

Answer

Does a filehandle have its own type name?

IO.

Technically, an IO object can have three handles associated with it: An input file handle, an output file handle, and a directory handle (e.g. created using opendir). 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 glob  (Returned by open $fh)
IO::File=IO(0x1175a60)    # Reference to IO object.