http://www.perlmonks.org?node_id=374924


in reply to Reference Notation

Assuming that $href->{$dn}->{$mail} contains an array reference, then your choices are:

@{ $$href{dn}{mail} }; # or @{ $href->{dn}{mail} }; # or @{ $href->{dn}->{mail} };

to refer to the whole array and

$$href{$dn}{mail}[0]; # or $href->{dn}{mail}[0]; # or $href->{dn}->{mail}->[0]; ## yawn! :) # or ...

to refer to the individual elements.

However, there is a useful though slightly obscure way of simplifying the access to deeply nested structures without needing to copy data into temporary variables.

## Make the localised glob *mail ## act as an alias to the nested array { local *mail = $ref->{dn}{mail}; print @mail; ## Print the whole array print $mail[0]; ## The first element } ## *mail reverts to it's old value here.

It's especially useful when you have to do a whole bunch of accesses to the elements of some deeply nested structureal element.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Reference Notation
by eric256 (Parson) on Jul 16, 2004 at 19:05 UTC

    Whats the benifit of a glob like that as opposed to a reference? Since it contains an array ref couldn't you just do. my $mail = $ref->{dn}{mail}; print @$mail;


    ___________
    Eric Hodges

      The benefits are @mail -v- @$mail and $mail[ 0 ] -v- $mail->[ 0 ], if the array contains just scalars, Where it really comes into it's own is if the array contains further nested structures.

      local *mail = ... $mail[ 3 ]{ recipient } = $mail[ 2 ]{ sender }; ## is easier on the eyes and fingers than $mail->[ 3 ]{ recipient } = $mail->[ 2 ]{ sender };

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

        Maybe your eyes :) Mine like $mail->[3]->{recipient} =  $mail->[2]->{recipient}; but that might be a flaw of mine. Cool to know that you can glob a @array onto an array reference though.


        ___________
        Eric Hodges