For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2008 > question about typeglob









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author question about typeglob
Enjoy_Life

2008-01-23, 7:06 pm

hi.

i found typeglob and symbol table is quite difficult for me.
so i want someone to help me on it.

see the code below:

$foo = "Some value";
*foo{PACKAGE}; #(1)
*{*foo}{PACKAGE}; #(2)

why the value of (1) and (2) are equal?
does that means foo is equals to *foo?

thanks


--
****************************************
******
** Happy Everyday **
****************************************
******

Jeff Pang

2008-01-23, 7:06 pm



-----Original Message-----
>From: Enjoy_Life <bobdyln@gmail.com>


>
>$foo = "Some value";
>*foo{PACKAGE}; #(1)
>*{*foo}{PACKAGE}; #(2)
>
>why the value of (1) and (2) are equal?


This is because *foo == *{*foo}.

>does that means foo is equals to *foo?
>


No.'foo' is a key in symbol table,whose corresponding value is *foo.
see this test:

$ perl -le 'our $foo;print $::{foo}'
*main::foo

here *main::foo == *foo. %:: is the symbol table entry.

Regards,
Jeff Pang
Enjoy_Life

2008-01-23, 7:06 pm

thanks to your quickly answer.

now, i know :
is *foo itself a hash too? *foo{SCALAR},*foo{ARRAY}...are reference to
$foo, @foo,....
is that right?

and i want to know why *foo == *{*foo}.

thanks very much.



On Jan 23, 2008 11:43 PM, Jeff Pang <pangj@earthlink.net> wrote:
[color=darkred]
>
>
> -----Original Message-----
>
>
> This is because *foo == *{*foo}.
>
>
> No.'foo' is a key in symbol table,whose corresponding value is *foo.
> see this test:
>
> $ perl -le 'our $foo;print $::{foo}'
> *main::foo
>
> here *main::foo == *foo. %:: is the symbol table entry.
>
> Regards,
> Jeff Pang
>




--
****************************************
******
** Happy Everyday **
****************************************
******

John W. Krahn

2008-01-23, 7:06 pm

Enjoy_Life wrote:
> hi.


Hello,

> i found typeglob and symbol table is quite difficult for me.
> so i want someone to help me on it.


Get in your time machine and travel back 15 years when typeglobs and the
symbol table were relevant. There is little documentation for these
things as modern versions of Perl don't rely on them as much as
Perl3/Perl4 did.


> see the code below:
>
> $foo = "Some value";
> *foo{PACKAGE}; #(1)
> *{*foo}{PACKAGE}; #(2)


$ perl -le'$foo = "Some value"; *bar = *foo; print $bar; use
Data::Dumper; print Dumper *bar{PACKAGE}'
Some value
$VAR1 = 'main';

$ perl -le'$foo = "Some value"; *bar = *foo; print $bar; use
Data::Dumper; print Dumper *bar{SCALAR}'
Some value
$VAR1 = 'Some value';

$ perl -le'$foo = "Some value"; *bar = *foo; print $bar; use
Data::Dumper; print Dumper *{*{*{*{*bar}}}}{PACKAGE}'
Some value
$VAR1 = 'main';

$ perl -le'$foo = "Some value"; *bar = *foo; print $bar; use
Data::Dumper; print Dumper *{*{*{*{*bar}}}}{SCALAR}'
Some value
$VAR1 = 'Some value';


> why the value of (1) and (2) are equal?


Because that is the way they work.

> does that means foo is equals to *foo?


foo is a bareword that could be either a subroutine name or a filehandle
or a directory handle or a format name or a label name or a text string,
depending on context.

*foo is a typeglob that represents any variable type in the current package.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Jeff Pang

2008-01-23, 10:07 pm



-----Original Message-----
>From: Enjoy_Life <bobdyln@gmail.com>
>Sent: Jan 24, 2008 12:13 AM
>To: Jeff Pang <pangj@earthlink.net>
>Cc: beginners@perl.org
>Subject: Re: question about typeglob
>
>thanks to your quickly answer.
>
>now, i know :
>is *foo itself a hash too?


No,it's just a glob.
*foo{HASH} means a hash.
*foo{PACKAGE} means the current package.
*foo{SCALAR} means a scalar.
*foo{ARRAY} means an array,etc.


>*foo{SCALAR},*foo{ARRAY}...are reference to
>$foo, @foo,....
>is that right?


Yes. can give a test:

$ perl -le 'our $foo="abc";print ${*foo{SCALAR}}'
abc

$ perl -le 'our @foo=(1,2,3);print @{*foo{ARRAY}}'
123

>
>and i want to know why *foo == *{*foo}.
>


As John said, this is the way perl works.
Actually symbol table is not so much worthy to research in this day.
Even if you write advanced perl module you don't need to know much about it.
I paid some time to study it when I researched modperl and the XS,but I never used it.


Regards,
Jeff Pang
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com