Typeglobs and References

Discussion in 'Perl Misc' started by Krishna Chaitanya, Sep 15, 2010.

  1. Hi everybody,

    I've read in Perl books that when used as lvalues, typeglobs are
    "equivalent" to references...something like this is constantly quoted
    as an example of selective aliasing:

    *b = \$a; # aliases $b to $a but leaves @b,%b,etc untouched

    Also, I've read about the *foo{THING} notation...accessing $a as:

    print ${*a{SCALAR}}; # prints the value of $a

    But what is this following code I see in some places:

    print ${*a}; # ALSO prints the value of $a

    I am confused.....how is ${*a} equivalent to ${*a{SCALAR}} ? If these
    2 are equivalent, why follow the *foo{THING} notation at all...? Seems
    like the '{THING}' part is quite useless here? Am I wrong, or am I
    missing anything subtle/obvious here?

    Pls. enlighten.....thanks a lot.

    -Chaitanya
    Krishna Chaitanya, Sep 15, 2010
    #1
    1. Advertising

  2. Krishna Chaitanya

    C.DeRykus Guest

    On Sep 15, 6:27 am, Krishna Chaitanya <> wrote:
    > Hi everybody,
    >
    > I've read in Perl books that when used as lvalues, typeglobs are
    > "equivalent" to references...something like this is constantly quoted
    > as an example of selective aliasing:
    >
    > *b = \$a; # aliases $b to $a but leaves @b,%b,etc untouched
    >
    > Also, I've read about the *foo{THING} notation...accessing $a as:
    >
    > print ${*a{SCALAR}}; # prints the value of $a
    >
    > But what is this following code I see in some places:
    >
    > print ${*a}; # ALSO prints the value of $a
    >
    > I am confused.....how is ${*a} equivalent to ${*a{SCALAR}} ? If these
    > 2 are equivalent, why follow the *foo{THING} notation at all...? Seems
    > like the '{THING}' part is quite useless here? Am I wrong, or am I
    > missing anything subtle/obvious here?
    >
    > Pls. enlighten.....thanks a lot.
    >
    > -Chaitanya



    The sigil '$' in ${*a} provides the context that
    enables the parser to lookup the glob's SCALAR
    slot. In this setting, you can omit the SCALAR
    but, within docs, the tag provides a quick, clear
    way of identifying an individual glob slot. This
    becomes very handy in perlref in the discussions
    of ref/glob equivalencies for instance:

    $scalarref = *foo{SCALAR};
    $arrayref = *ARGV{ARRAY};
    ...

    See: perldoc perlref

    BTW, ${\$a} is also equivalent to ${*a}. You can
    usually just use a reference and rarely need to
    deal with the glob notation at all.

    --
    Charles DeRykus
    C.DeRykus, Sep 15, 2010
    #2
    1. Advertising

  3. Thanks a lot, Ben and Charles. It's clearer to me now...and yeah I've
    gone through perlref and perlmod just now.

    Ben, what I'm trying to do is to understand code someone else wrote -
    which, for reasons best known to him, uses typeglobs in many
    places...I want to understand this concept and replace that code with
    reference-based code as much as possible (but certainly not blindly,
    stupidly or without realizing why he used globs at all...)
    Krishna Chaitanya, Sep 16, 2010
    #3
  4. On 2010-09-15, C.DeRykus <> wrote:
    > BTW, ${\$a} is also equivalent to ${*a}.


    Of course, it is NOT.

    >perl -wle "$a=12; my $a=2; print ${\$a}; print ${*a}"

    2
    12

    Hope this helps,
    Ilya
    Ilya Zakharevich, Sep 16, 2010
    #4
  5. Krishna Chaitanya

    C.DeRykus Guest

    On Sep 16, 1:09 am, Ilya Zakharevich <> wrote:
    > On 2010-09-15, C.DeRykus <> wrote:
    >
    > > BTW, ${\$a} is also equivalent to ${*a}.

    >
    > Of course, it is NOT.
    >
    > >perl -wle "$a=12; my $a=2; print ${\$a}; print ${*a}"

    >
    > 2
    > 12
    >


    Agreed, watch your feet and no lexicals "need apply"
    for this global position.


    --
    Charles DeRykus
    C.DeRykus, Sep 16, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    415
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    435
    Victor Bazarov
    Nov 10, 2004
  3. Bernard Chan

    Typeglobs, Anonymous Filehandles

    Bernard Chan, Mar 7, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    84
    Bernard Chan
    Mar 7, 2006
  4. Veli-Pekka Tätilä
    Replies:
    9
    Views:
    149
    Ben Morrow
    Aug 4, 2006
  5. Krishna Chaitanya

    Typeglobs vs References

    Krishna Chaitanya, Mar 25, 2009, in forum: Perl Misc
    Replies:
    12
    Views:
    142
    Peter Scott
    Mar 28, 2009
Loading...

Share This Page