Typeglob Qs

K

kj

What is the connection, if any, between typeglobs and references
to package variables? The following debugger interaction suggests
that they are very similar:

DB<1> $x = 'foobar'
DB<2> $y = *::x
DB<3> p $$y
foobar
DB<4> $z = \$::x
DB<5> p $$z
foobar

Is this similarity of behavior superficial?

Also, how can one retrieve the dirhandle, socket, and format items
from a typeglob. I'm aware of the *x{THING} syntax, but it's not
clear to me what value to use inside the curlies to access the
items listed.

Thanks!

kj

kj
 
F

Fabian Pilkowski

* kj said:
What is the connection, if any, between typeglobs and references
to package variables? The following debugger interaction suggests
that they are very similar:

DB<1> $x = 'foobar'
DB<2> $y = *::x
DB<3> p $$y
foobar
DB<4> $z = \$::x
DB<5> p $$z
foobar

Is this similarity of behavior superficial?

Here, the typeglob is doing more. Not only the SCALAR slot in the symbol
table entry is altered. Example for ARRAY:

#!/usr/bin/perl -w
@x = qw( 1 2 3 );
$x = 'foobar';

$y = *x;
print $$y, "\n";
print @$y, "\n";

$z = \$x;
print $$z, "\n";
print @$z, "\n";
__END__

After doing "$y=*x" you could also run a subroutine &x() by calling
&$y(). You say something like: "all that is accessible via 'x' I want
access via $y too".

In the second case you just say: "make $z a reference to the scalar slot
of x". AFAIK it has the same effect as calling "$z=*x{SCALAR}".
Also, how can one retrieve the dirhandle, socket, and format items
from a typeglob. I'm aware of the *x{THING} syntax, but it's not
clear to me what value to use inside the curlies to access the
items listed.

The way I understand `perldoc perlref` the THING could be one of
SCALAR, ARRAY, HASH, CODE, IO, or GLOB. Nothing else. *foo{THING}
returns a reference to the THING slot in *foo. I read (shortened):

*foo{IO} ... It returns the IO handle, used for file handles,
sockets, and directory handles.

In other words there is only one IO handle (singular) for all these
types. The referenced page `perldoc -f opendir` says:

DIRHANDLEs have their own namespace separate from FILEHANDLEs.

Combining all of this together: I think you could grab only one IO
reference from a specific symbol table entry. But you could use the
*same* one for different things like open/opendir/socket at the same
time. There is no problem due to separated namespaces, and Perl is
deciding which handle is to use. Feel free to make a test script for
this.

regards,
fabian
 
S

Sisyphus

kj said:
What is the connection, if any, between typeglobs and references
to package variables? The following debugger interaction suggests
that they are very similar:

DB<1> $x = 'foobar'
DB<2> $y = *::x
DB<3> p $$y
foobar
DB<4> $z = \$::x
DB<5> p $$z
foobar

Is this similarity of behavior superficial?

A Devel::peek::Dump() can be useful here:

--- try.pl ---
use warnings;
use Devel::peek;

$x = 'foobar';
$y = *::x;
$z = \$::x;

Dump($y);
print "#############\n";
Dump($z);
__END__

produces:

SV = PVGV(0x8eadc4) at 0x3f5e38
REFCNT = 1
FLAGS = (GMG,SMG,FAKE,MULTI)
IV = 0
NV = 0
MAGIC = 0x893124
MG_VIRTUAL = &PL_vtbl_glob
MG_TYPE = PERL_MAGIC_glob(*)
MG_OBJ = 0x3f5e38
NAME = "x"
NAMELEN = 1
GvSTASH = 0x3f5040 "main"
GP = 0x8a208c
SV = 0x3f5e14
REFCNT = 2
IO = 0x0
FORM = 0x0
AV = 0x0
HV = 0x0
CV = 0x0
CVGEN = 0x0
GPFLAGS = 0x0
LINE = 4
FILE = "try.pl"
FLAGS = 0x2
EGV = 0x3f5e44 "x"
#############
SV = RV(0x8db044) at 0x3f5e8c
REFCNT = 1
FLAGS = (ROK)
RV = 0x3f5e14
SV = PV(0x3f5ff4) at 0x3f5e14
REFCNT = 2
FLAGS = (POK,pPOK)
PV = 0x8933c4 "foobar"\0
CUR = 6
LEN = 7

Cheers,
Rob
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top