Ltf4an Ltf4an - 8 months ago 50
Perl Question

Why doesn't a filehandle need a sigil in Perl?

In Perl, filehandle is a data type, and I'd expect a variable of such type has some kind of sigil prefix. However, the following code (2nd open) shows that it's not the case

open my $fileHandle, '>', "out.txt";
open FH, '>', "out2.txt";

I found the 2nd form confusing/inconsistent. What is the reason for allowing the 2nd form?

Answer Source

The second form is allowed because it is the original one. You used to only be able to refer to filehandles by the name of the glob that contained them. Now filehandles can be the name of a glob or a reference to a glob wrapped around a filehandle object or a reference to a filehandle object. The latter two can be stored in a scalar (and so can be lexically scoped).

You should avoid using globs as filehandles in new code.