Generally speaking, you do need a sigil when referencing a glob.
my $fh = STDOUT; # XXX Short for: my $fh = "STDOUT";
my $fh = *STDOUT; # ok
However, functions that expect a glob (e.g. open
, print
, readline
aka <>
, etc) make it optional.
print STDOUT "foo\n"; # Short for: print *STDOUT "foo\n";
You can approximate this with the *
prototype.
sub foo { }
sub bar(*) { }
foo(STDOUT); # XXX Fails when using "use strict;"
bar(STDOUT); # ok
What is the reason for allowing the 2nd form?
The second form (which uses a global symbol) predates the support for open(my $fh, ...)
introduced in 5.6. In fact, it predates the existence of lexical (my
) variables. Since one should avoid global variables whenever possible, the open(FH, ...)
is discouraged.