Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Reference Notation

by Anonymous Monk
on Jul 16, 2004 at 06:06 UTC ( [id://374917]=perlquestion: print w/replies, xml ) Need Help??

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

I normally use references in the most basic way--like passing hashes or arrays to subroutines by reference. I've been using Net::LDAP extensively for over a year and just today noticed the as_struct() method of Net::LDAP::Search.

This method seems save considerable code as opposed to the entries() method. When using entries(), I end up creating my own HoHs via $entry->get_value( 'attribute' ). The only nice thing about building my own HoH is that I'm comfortable using $hash{dn}{mail} constructs. Using the as_struct() method means using either @{ $$href{$dn}{mail} } or @{ $hash{$dn}{mail} }.

Is there a shorthand notation for the above or am I making too big a deal out of using the extra outer @{} constructs? I looked closely at References quick reference but don't grok all of it.

Thanks.

Replies are listed 'Best First'.
Re: Reference Notation
by BrowserUk (Patriarch) on Jul 16, 2004 at 06:47 UTC

    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://374917]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-24 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found