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

J

Jaap Karssenberg

But if reference to a typeglob is equivalent to a reference to
: whatever type with that name I want, it should be able to say:
:
: $$fh = 10;
: print "$$fh";

I can't tes tit right now but I think you can assign to it
like this :

*$fh = \'10'

you need to force the value to be understood as a scalar reference
instead of a scalar
 
B

Brian McCauley

ddtl said:
But why not use $@$fh or $%$fh - it shouldn't be ambiguous -
the first $ indicates that fh is a scalar.

That's first counting from the right or from the leaves of the
parse tree. While in execution order the leafmost part of the
parse tree is usually first most humans when reading a L2R rendered
language will read "first" as meaning leftmost.
The second funny character
indicates that we dereference scalar and getting to the typeglob

If you wanted to unambiguously get to a typeglob it would be a *.

However there's a grey area between GLOBs and SCALARs - this is for
historical reasons. Just like references can be handled as scalars so
can GLOBs. So for this reason you can use $ even though I'd always
use * for the sake of clarity. You cannot use a @ or a % because $foo
is not usable as a hashref or an arrayref.

my $foo = \*bar; # $foo contains a GLOBref

# Try to dereference $foo as a GLOB
print *$foo; # prints *main::bar
print ref \*$foo; # prints GLOB

# Try to dereference $foo as a scalar but actually get a GLOB
print ref \$$foo; # prints GLOB

# Try to dereference $foo as an array
print ref \@$foo; # error - Not an ARRAY reference
(typeglob is either a scalar, or array or hash, so every funny
character should do - why give special privilege to $?).

No. A reference is a special type of scalar value that points to a
single thingy. A (fake) glob is a special type of scalar value that
points to one thingy of each type.

If $foo was a reference to a reference to a hash you'd use %{${$foo}}
or just %$$foo.

If $foo was a reference to a glob to and you want the hash part of the
glob I'd use %{*{$foo}} but because GLOBs can sometimes be treated as
SCALAR you can still just use %$$foo.


--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top