Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: The Concept of References

by polettix (Vicar)
on Apr 14, 2005 at 15:13 UTC ( #447830=note: print w/ replies, xml ) Need Help??


in reply to The Concept of References

Due to my C++ roots, I really find it more useful to think of Perl References as if they were pointers more than references, because of what my (limited) brain believes a reference should be. Apart from the (correct) pointer arithmetics stuff, the semantic of a Perl reference requires an indirection, while in C++ a reference is just some kind of alias for another variable/object and requires no indirection at all.

That's also why I'll feel less comfortable with Perl 6, which I understand will jump from "->" to "." for dereferencing in objects. But this is a mined field, 'cause I know really nothing about Perl 6!

Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

Don't fool yourself.


Comment on Re: The Concept of References
Re^2: The Concept of References
by revdiablo (Prior) on Apr 14, 2005 at 16:29 UTC

    Another difference between Perl's references and pointers ala C, is that a reference knows what it refers to. It knows what kind of reference it is. It's not just a reference, but an array reference or a hash reference, etc.

    Update: my lack of C knowledge should be self-evident. Thanks to frodo72 for explaining why I'm wrong.

      One could argue that a pointer in C, e.g. to a struct tm, knows what it points to, so that you can write:
      struct tm *p = &data; localtime(p); printf("Year: %d\n", p->tm_year);
      which, assuming you have a hash filled with the very same parameters, resembles $p->{'tm_year'} very much.

      Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

      Don't fool yourself.

        Correct me if I'm wrong, but I was under the impression that the pointer itself doesn't know whether it's a pointer to a struct, or anything else. The pointer just points to a spot of memory. It's the syntax that you use that determines how the memory gets used.

        Perl's references, on the other hand, know what they're referring to. If you try to use a hash reference as an array reference, perl can look at the reference type and say, "wait a minute, that's not an array reference!"

        I don't know how much difference this makes in terms of usage, but it's certainly a difference between the two constructs.

        One could argue that a pointer in C, e.g. to a struct tm, knows what it points to

        In C the programmer tells the pointer what it points to. In Perl the reference knows what it points to. It's an important if subtle difference.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-12-27 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls