Search code examples
perlreffilehandletypeglob

Why ref does not return 'GLOB'?


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?


Solution

    • 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".»