3

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?

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158

2 Answers2

8
  • 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".»
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Which module you can advice to dump glob? – Eugen Konkov Oct 01 '15 at 13:13
  • `use Data::Dump qw/ pp /; print pp \*F` does not show that glob has {IO} – Eugen Konkov Oct 01 '15 at 13:21
  • 4
    A [glob](http://perl5.git.perl.org/perl.git/blob/5a4fed095144d9c2f728401b3a0938f80aca4bcc:/gv.h#l11) is a C struct. As always, if you want to look at the internals of a Perl variable, use Devel::Peek. `perl -MDevel::Peek -e'Dump(*STDOUT)'`. I think you want to read [illguts](http://search.cpan.org/dist/illguts/) – ikegami Oct 01 '15 at 13:23
  • No, that is not the thing I want. Want to see the IO object's properties: autoflush flag, the cursor, attached layers etc. so that it has been shown like HASH were – Eugen Konkov Oct 01 '15 at 15:58
  • 3
    You said you wanted to dump a glob, not an IO object. Dumping a glob doesn't recurse, but you can dump the IO object itself. `perl -MDevel::Peek -e'Dump(*STDOUT{IO})'`. There is no cursor (that's on the OS side), and the layers can be shown using `PerlIO::get_layers`. – ikegami Oct 01 '15 at 16:07
4

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.

choroba
  • 231,213
  • 25
  • 204
  • 289