10. References

Perl allows one to refer to the location of a certain variable in memory. An expression that holds such a location is called a reference. Those that are familiar with C's or Pascal's pointers may think of references as pointers. There are however, two fundamental differences:

  1. There is no reference arithmetics in perl. If for example, a reference points to the fifth element of an array, then adding 1 to it will not refer you to the sixth element. In fact, adding or substracting integers from references is possible but quite meaningless.
  2. A reference is guaranteed to remain the same even if an array or a string are resized. In C, reallocing an array yields a completely different pointer.

Perl distinguishes between an array or a hash and a reference of it. The reference of any array may be taken, and a reference to an array may always be converted to its elements, but there is still a difference in functionality.

The best way to change a variable in a different scope (such as inside a different function) is to pass its reference to the function. The called function can then dereference the variable to access or modify its value.


Written by Shlomi Fish