Getting to variables contained in a typeglob referenced by a scalar.

From: ddtl (this.is_at_invalid)
Date: 06/30/04


Date: Wed, 30 Jun 2004 19:31:10 +0200


Hello,

After 'open' is called in the following way:

open my $fh, $file;

'$fh' contains a reference to a filehandle, i.e., somewhere in the 'open'
function there is probably a following assignment:

$fh = \*SOME_TYPEGLOB;

(and somehow that typeglob is anonymous (?), so the filehandle is
also anonymous).

If I understand correctly, it actually means that '$fh' contains
a reference to a typeglob, because wherever there is a filehandle,
it can be substituted for a typeglob.

Than, if we want to get to the variables inside that typeglob, we
have to do it in the following way:

*$fh - access the fileglob
*$fh->{SCALAR} - access a reference to a scalar (typeglob is also a special
                hash)
${*$fh->{SCALAR}} - access the scalar itself.

But "Programming Perl" (14.4.1) mentions another, easier way -
just use $$$fh (or @$$fh or %$$fh, etc.).

I don't understand, though, how it works - what do we access when
$$fh is used - there is no scalar reference inside '$fh'?
The book doesn't mention it as some new way of using typeglobs/references,
which probably means that it should be clear from the previous chapters
why and how it works, and it seems that I missed the point.

The question is, then: how and why $$$fh etc. work, and where is an
explanation for it in the book (or just in the documentation)

ddtl.



Relevant Pages

  • Re: Getting to variables contained in a typeglob referenced by a scalar.
    ... the first $ indicates that fh is a scalar. ... indicates that we dereference scalar and getting to the typeglob ... of a reference - first reference to a typeglob, ...
    (comp.lang.perl.misc)
  • Re: Symbol Table and References
    ... @st is an array and so is stored internally in an array value.The ... a reference points ... typeglob is simply a holder and has a 'slot' for each kind of 'value' ... scalar, array, hash, code, IO handle and format. ...
    (comp.lang.perl.misc)
  • Re: <> and filehandles in hashes
    ... and it also works fine if I put the filehandle in a scalar ... If what the angle brackets contain is a simple scalar variable ... filehandle to input from, or its typeglob, or a reference to the ... it is interpreted as a filename pattern to ...
    (comp.lang.perl.misc)
  • Re: Trying to combine timeout with getting programs pid
    ... It's being piped to the EXTRACT filehandle. ... A filehandle is not a scalar, any more than it is an array or a hash. ... It is a separate type of Perl variable. ... You can, however, store a reference to a filehandle in a scalar ...
    (comp.lang.perl.misc)
  • Re: Getting to variables contained in a typeglob referenced by a scalar.
    ... >regard a typeglob reference as a reference to any type at the same time. ... >scalar type. ... But if reference to a typeglob is equivalent to a reference to whatever type ...
    (comp.lang.perl.misc)