Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

difference between pointer and referrence in perl

by sir_com (Acolyte)
on Aug 26, 2009 at 20:51 UTC ( #791456=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

Is there any legitimate difference between C pointers and referrence in perl? or it's like the term gets used interchangably. As per my understanding both stores address of something (array,hash or scalar).

going through the pdf Begining perl by Simon cozens, it says that "referrence in perl and pointers in C are not exactly same" but going through the link below says that both are same and you can use the terms pointer and reference interchangeably. http://www.bjnet.edu.cn/tech/book/perl/ch18.htm Is there really a difference exists?

Thanks, sir_com
  • Comment on difference between pointer and referrence in perl

Replies are listed 'Best First'.
Re: difference between pointer and referrence in perl
by GrandFather (Saint) on Aug 26, 2009 at 20:59 UTC

    Perl's references are more than just C pointers. Perl reference counts objects so the reference object that holds onto the 'address' of another object is more like a C++ smart pointer with a whole world of semantics hidden under a fairly conventional looking syntax. Most of the time you needn't worry about the extra semantics, it just works. Occasionally you run into memory leaks with interesting structures that contain reference loops that have to be forcibly broken before the Perl can dispose of the object.


    True laziness is hard work
Re: difference between pointer and referrence in perl
by JavaFan (Canon) on Aug 26, 2009 at 21:13 UTC
    They are not the same. Sure, they both "point" to something, but that's about it. In C, a pointer is just an integer - you can add to it: you can say, don't give me the thing it's pointing at, give me whatever is 5 places further. Regardless whether 5 places further actually has stored something that's meaningful.

    You can't do that in Perl. You cannot stuff an integer in a reference and have Perl retrieve what's in that memory address. And while you can add something to a reference, the result is a number - not a reference. You cannot dereference the result of the addition.

    IMO, saying that "Perl references" and "C pointer" are kind of the same is only useful if you know enough Perl and C to know the differences.

      Thanks for explaining clearly with example
Re: difference between pointer and referrence in perl
by Marshall (Canon) on Aug 26, 2009 at 22:45 UTC
    A C pointer points to a memory location. That's pretty much it.
    What will happen when you deference that pointer depends upon what other 'C' statements have defined the "thing" that lives at that memory location. The 'C' language would be worthless if it didn't have the abilty to increment a pointer to memory by an unknown number of bytes between different things in memory, based upon a sort of definition of the memory layout. In other words, 'C' "knows" how big an individual "thing" is.

    What 'C' doesn't know is: how many of those "things" exist, but Perl does know. A Perl reference points to a "Perl thing" like: a hash table or an array or a scalar or even a Perl reference to one of these things.

    Unlike 'C', a Perl "thing" knows how big it is.

    The most common error in 'C' is "off by one" array indicies. Perl helps a lot with these iterators. For example: foreach my $x(@some_array). I don't need to know how big @some_array is, I will get an x for every value in @some_array. In 'C', I have to know explictly how "big it is" or have some other way of finding out (like a signal value in that array).

      Hi Monks, Thanks to you all for sharing your knowledge with me. sir_com
Re: difference between pointer and referrence in perl
by bv (Friar) on Aug 26, 2009 at 21:07 UTC

    For the most part, they act the same. Probably the biggest difference I have noticed is that you don't have to worry about dereferencing a reference after the object referred to goes out of scope:

    my $ref; { my $var = 5; $ref = \$var; } # prints 5 print $$ref;
    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
Re: difference between pointer and referrence in perl
by biohisham (Priest) on Aug 26, 2009 at 21:44 UTC
    In addition to arrays, hashes, and scalar references you also have:
    1. subroutine references.
    2. typeglob references.
    3. references to lists (distinct from references to arrays).
    4. IO references.
    5. LVALUE references.

    one distinguishing feature of Perl, not available in C involves DYNAMIC creation of data structures and prevents excessive typing, this feature is called Autovivification, in reference view, a reference springs to existence if it is dereferenced under the assumption that it exists. With this in mind, you would notice that Perl references and C pointers are not exactly the same.

    EXAMPLE: (Note the reference is held in the scalar value $reference!) $$reference=5; #Dereferencing before creating a reference. print "$$reference\n"; #Creation of $reference autovivifically. print "$reference\n";

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2021-05-10 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (104 votes). Check out past polls.

    Notices?