Why ref does not return 'GLOB'?

351 Views Asked by At

I want to understand typeglobs better and write small programm:

use Symbol;

open F, '>', \my $var;
t( F );
t( \F );
t( *F );
t( \*F );
sub t {
   my $fh =  shift;
   print ">>$fh<<" . ref( $fh ) ."\n";
}

The output is:

>>F<<
>>SCALAR(0x1e67fc8)<<SCALAR
>>*main::F<<
>>GLOB(0x1e53150)<<GLOB

Why GLOB is returned only in last case?

2

There are 2 best solutions below

5
ikegami On BEST ANSWER
  • t( F ): A string[1] isn't a reference to a glob.
  • t( \F ): A reference to a string isn't a reference to a glob.
  • t( *F ): A glob isn't a reference to a glob.
  • t( \*F ): A reference to a glob is a reference to a glob.

  1. «A word that has no other interpretation in the grammar will be treated as if it were a quoted string. These are known as "barewords".»
0
choroba On

Without strict, identifiers without other meaning are interpreted as barewords, and barewords produce strings. That explains the first two lines (cf. print \"F" for the second one).

The interpolation of globs (3rd line) is documented in perlref.

*foo{NAME} and *foo{PACKAGE} are the exception, in that they return strings, rather than references. These return the package and name of the typeglob itself, rather than one that has been assigned to it. So, after *foo=*Foo::bar, *foo will become "*Foo::bar" when used as a string, but *foo{PACKAGE} and *foo{NAME} will continue to produce "main" and "foo", respectively.