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

Re: Learning to *really* love references

by LanceDeeply (Chaplain)
on Jul 30, 2002 at 15:17 UTC ( [id://186229]=note: print w/replies, xml ) Need Help??


in reply to Learning to *really* love references

My question is:

What's the commonly accepted syntax to do this? I've never used:
foreach my $record (@{$array_ref}) {

The notation I'm comfortable w/ is:
foreach my $record ( @$array_ref ) {

Is there an accepted standard?


astaines:
since you said you're still learning about refferences - you might want to try deref'ing them like so:
my $arrayElement = $$arrayRef[0]; my $hashValue = $$hashRef{someKey};

Also, you may want to checkout wantarray for returning array's or their references to the caller.

Hope this helps

Replies are listed 'Best First'.
Re(2): Learning to *really* love references
by FoxtrotUniform (Prior) on Jul 30, 2002 at 15:44 UTC
      my $arrayElement = $$arrayRef[0]; my $hashValue = $$hashRef{someKey};

    I prefer:

    my $array_element = $array_ref->[0]; my $hash_value = $hash_ref->{some_key};

    Using arrow notation makes it easier for me to recognize that the thingys in question are references (I find that the extra leading sigil gets lost in the code soup to my eyes). It also makes doing nested dereferences easier:

    my $foo = $AoH_ref->[0]->{some_key};

    although you'd be better off doing:

    my $hash_ref = $AoH_ref->[0]; my $foo = $hash_ref->{some_key};

    in most cases.

    --
    The hell with paco, vote for Erudil!
    :wq

Re: Re: Learning to *really* love references
by tmiklas (Hermit) on Jul 30, 2002 at 22:32 UTC
    Well...

    Long time ago i used to write my code like this:
    my $arrayElement = $$arrayRef[0]; my $hashValue = $$hashRef{someKey};
    but when I tried to debug or simply read it a few months after coding, it took me a long time to make my brain think the same, wired way as when I wrote the code ;-)

    Since then I use
    my $arrayElement = ${$arrayRef}[0]; my $hashValue = %{$hashRef}{key};
    or
    my $arrayElement = $arrayRef->[0]; my $hashValue = $hashRef->{key};
    and it depends of the mood ;-)

    Anyway IMHO the $$something notation is the worst to read or understand again after some time... If you code alone it's up to you which one to choose, but if I'm a part of a team I just try to be polite, writing my code more readable and easy to understand to my co-workers...

    Greetz, Tom.
Re: Re: Learning to *really* love references
by astaines (Curate) on Jul 30, 2002 at 22:48 UTC

    The notation is what I always use - I find it easier to stick to one form throughout. Don't get me wrong I appreciate Perl's flexibility, but consistency makes my life simpler. I make no claim for superiority here! However, I do agree that the $$ notation is hard to follow, and I never use it


    --
    Anthony Staines

    PS -thanks for the tip about deref'ing - I'll try it out next time.

Re: Re: Learning to *really* love references
by sporte01 (Acolyte) on Jul 31, 2002 at 20:25 UTC
    Until you want to do fancy things. What if you have an object that contains a hash or array ref? Ie...
    $this = { _hashRef => {}, _arrayRef => [], };

    Then you are stuck doing....
    $x = ${$this->{'_hashRef'}}->{'blowMeDown'} @x = reverse @{$this->{'_arrayRef'}};

    And that's one to grow on.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2024-04-25 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found