targetsmart has asked for the wisdom of the Perl Monks concerning the following question:

Just i am in the process of understanding typeglobs
I have a code like below
print "-"x80,"\n"; $a = 1; @a = (1..10); %a = (10..20); open(a,"/etc/passwd") or die "can't open /etc/passwd : $!"; sub a { "TEST" }; format a = @>>>>>>>> . &DumpGlob(*a); print "-"x80,"\n"; sub DumpGlob { my $symbol = shift; if(defined *{$symbol}{SCALAR}) { print "SCALAR defined >",${*{$symbol}{SCALAR}},"<\n"; } if(defined *{$symbol}{ARRAY}) { print "ARRAY defined >",@{*{$symbol}{ARRAY}},"<\n"; } if(defined *{$symbol}{HASH}) { print "HASH defined >",%{*{$symbol}{HASH}},"<\n"; } if(defined *{$symbol}{IO}) { print "IO defined >",*{$symbol}{IO},"<\n"; } if(defined *{$symbol}{CODE}) { print "CODE defined >",*{$symbol}{CODE},"<\n"; } if(defined *{$symbol}{FORMAT}) { print "FORMAT defined >",*{$symbol}{FORMAT},"<\n"; } if(defined *{$symbol}{GLOB}){ print "GLOB defined >",${*{$symbol}{GLOB}},"<\n"; } } the output is like this SCALAR defined >1< ARRAY defined >12345678910< HASH defined >1819161710111213201415< IO defined >IO::Handle=IO(0x8154a2c)< CODE defined >CODE(0x81735b0)< FORMAT defined >FORMAT(0x81735f8)< GLOB defined >*main::a<
Now my question is what is the difference between *a and *a{GLOB}, if both are same then why do we have two pointing to same area?. Please clarify
UPDATE:
Pardon me
I have updated my question title from 'a doubt in typeglob' to 'what is the difference between *a and *a{GLOB}', Thanks

Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

Replies are listed 'Best First'.
Re: what is the difference between *a and *a{GLOB}?
by tilly (Archbishop) on Feb 01, 2009 at 01:28 UTC
    I think more explanation could be helpful.

    *foo is a way at getting at the symbol table entry for foo. As perlref explains, *foo has multiple slots, they are named SCALAR, ARRAY, HASH, CODE, IO, GLOB and FORMAT. Each of those is a reference to the appropriate kind of data structure. So *foo{SCALAR} is \$foo. *foo{ARRAY} is \@array \@foo, and so on. *foo{GLOB} is \*foo.

    When you use *foo and are unaware of what it is, things get very confusing, very quickly. If you print *foo you will see *main::foo. If you print ${*foo} you get whatever is in $foo. If you print @{*foo} you get at @foo, and so on. You can think of (it isn't, but you can think this way) *foo as being a weird kind of object that has been overloaded like crazy so that it can be dereferenced as several different data types.

    Think you've got it? OK, can you figure out why $foo is ${*foo} is ${*foo{SCALAR}} is ${*{*foo{GLOB}}}? If you can figure that out, then you understand what can be done with a symbol table entry.

    In case you understood that, let me add more detail about the symbol table. If you want to find all of the defined symbols you start by looking at the keys of %::. Keys that don't end in :: are associated with values that are symbol table entries. Any keys that end in :: are associated with child symbol tables. For instance if $foo::bar is defined then %:: will contain foo:: as a key. And %foo:: (equivalently %{$::{"foo::"}} - do you see why?) will contain bar as a key, and its value will be the symbol table entry for *foo::bar.

    Be warned that %:: is an alias for %main:: is an alias for %main::main:: is an alias for %main::main::main:: and so on. It is therefore not advisable to name a module "main". Also if you wish to recursively search the symbol table for something, be sure to avoid %main::main::.

    If you are still with me and can keep all of this straight, then congratulations. Relatively few Perl programmers actually understand the symbol tree in as much detail as you now do. If you got lost, then don't worry, that is to be expected. It took several tries for me to learn this to the point where I could manipulate the symbol table and get the right answer. It took longer until I convinced myself that I had an understanding of what was going on.

    UPDATE: johngg pointed out that I wrote \@array where I meant \@foo. Fixed.

Re: a doubt in typeglob
by Anonymous Monk on Jan 31, 2009 at 13:51 UTC
    Now my question is what is the difference between *a and *a{GLOB}, if both are same then why do we have two pointing to same area?. Please clarify

    The node title should be what is the difference between *a and *a{GLOB} not a doubt in typeglob.

    D:\>perl -e"warn *STDOUT; warn \*STDOUT; warn *STDOUT{GLOB}; *main::STDOUT at -e line 1. GLOB(0x2267ec) at -e line 1. GLOB(0x2267ec) at -e line 1. D:\>
    Of Symbol Tables and Globs, perlref
      The node title should be what is the difference between *a and *a{GLOB} not a doubt in typeglob.

      Had the OP entitled his post as: "I have a doubt about my understanding of typeglobs" would it still have offended your linguistic myopia?

      And isn't there a long literary tradition of shortening titles to the point where they become punchy phrases rather than fully grammatical sentences.

      There was a wonderful story on the BBC the other day (which I cannot link to because google seems to have been hacked Update:The story), about a female French Minister's choice to address the EU in English that was causing frowns in France. It went on to describe how English was becoming the defacto standard for international business discussions.

      It then pointed out that "English" was the wrong term for the language that is beginning to dominate; favouring a the term "Globlish". A new-speak like simplification of English, unencumbered by the arbitrary set of rules dreamt up by a few dozen aristocratic academics in the late 1900s that have constrained and hampered the learnability of English ever since.

      Instead, Globlish constrains itself to the communication of ideas and thoughts--rather than pedantic, elitist prose--through the use of a generic subset of six to seven thousand words and the complete relaxation of those archaic arbitrary rules of grammar. It went on to describe how a native English speaker stood listening in to a vociferous conversation (in Globlish) between three politicians from widely disparate geographical regions of the EU, and he (the native English speaker) seemed to be the only one who had trouble keeping up with the conversation.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        "linguistic myopia", I'm going to have to write that one down :-)

        meh.
        notwithstanding globlish, I think actual questions are better for SOPW node titles, better than "help, i'm seeking perl wisdom, I have a perl question, a doubt in about..."