Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: The Concept of References

by DrHyde (Prior)
on Apr 14, 2005 at 09:37 UTC ( #447674=note: print w/ replies, xml ) Need Help??


in reply to The Concept of References

One thing to bear in mind, and which may cause your readers some confusion, is that references are *not* pointers. While it is true that if you stringify a reference:

$a = \100; # $a is a reference to a scalar print $a;
you get what looks like an address, that is only a convenient way of uniquely representing the thingy that $a refers to. You can't manipulate that string to make it refer to something else like you can in C - that is, you can't do pointer arithmetic.

Sometimes I wish you could.


Comment on Re: The Concept of References
Download Code
Re^2: The Concept of References
by Forsaken (Friar) on Apr 14, 2005 at 12:10 UTC
    Just out of curiosity, could you come up with an example of how pointer arithmetic would be applicable for perl? I've been trying to think of a possible application but failed miserably, I'd be really interested to see your perspective.

    Remember rule one...
      You don't, because doing so would violate the guarantee that you cannot buffer overflow in Perl. But, there are good reasons why C gives you the rope to buffer overflow.

      Let's say you want to iterate through the characters of a string and do something with each character. In C, that's pretty simple. Strings are just arrays of characters and arrays are just fancy pointers.

      char string[] = "Hello"; char *ptr; fr ( ptr = string; *ptr; ptr++ ) { do_something_with( *ptr ); // This is a single char }
      Now, let's say you want to do the same thing in Perl. There's a few ways, but none are anywhere as efficient.
      foreach my $char ( split //, $string ) { ... } while ( $string =~ /(.)/g ) { my $char = $1; ... } for ( my $i = 0; $i <= length $string; $i++ ) { my $char = substr( $st +ring, $index, 1 ); ... }
      Brother dragonchild's example is a good one.

      It's not really a question of needing them, but that they are another tool available which let you approach problems in different ways. Sometimes a pointerish solution - like dragonchild's - would be easier to understand, just like sometimes an OO solution is easier, or a functional solution is easier.

      I do take the point that with pointers comes danger. It could probably be mitigated because perl knows more about the underlying data structures that you would be pointing at than C does - for instance, if you increment a pointer beyond the end of a string, perl can know that you've done that and so could automagically extend the string.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-08-23 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (174 votes), past polls