http://qs321.pair.com?node_id=11124048


in reply to Pointers and References

Perl doesn't have "pointers". Sure, under the hood they are everywhere, but it's not useful to think about pointers in Perl. The reason we talk about references is that there is some extraordinarily useful under the hood baggage associated with references. Perl uses reference counted memory management. A reference to something else is one type of data that a scalar variable can contain. You can have references to pretty much anything and you can use ref to tell you what you have a reference to. That sums up to, you hardly ever need to think about memory management in Perl and most things "just work".

So, in answer to question 1: no, it contains a reference (which might be an address of something, but you don't {and shouldn't} need to know what).

$ in Perl as a sigil means "give me a scalar value". In answer to 2: $$ means give me the scalar referred to by the scalar xxx. Because you can assign values to a scalar when you use $$ in an assignment you are assigning to the scalar referred to by the scalar. No pointers to be seen here.

A more useful example is:

use warnings; use strict; use v5.10; do { my $variable = 22; my $refVar = \$variable; $$refVar = 25; say "Look at that! \$variable now equals $variable"; my $two = 2; my ($sum, $diff) = sum_and_diff(5, $two); say "the sum of 5 and $two is $sum"; say "and the difference is $diff"; }; sub sum_and_diff { my ($lhs, $rhs) = @_; return $lhs + $rhs, $lhs - $rhs; }

Prints:

Look at that! $variable now equals 25 the sum of 5 and 2 is 7 and the difference is 3

Useful because it is a reminder that Perl doesn't need "output parameters" because it can return multiple values from a sub. References in Perl are most useful in building interesting structures which are a mixture of arrays, hashes and scalar values.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond