note
kcott
<p>G'day [id://11123295|Leudwinus],</p>
<blockquote>
<em>"I am still trying to wrap my head around ..."</em>
</blockquote>
<p>
You're using terms — <em>pointer</em>, <em>address</em>, <em>memory location</em> —
which I suspect you've picked up from one or more other languages.
You are then attempting to apply those terms to Perl, assuming they have the same meaning.
I think this may be the source of your problems.
</p>
<p>
I'd suggest the first thing to do would be to look at [https://perldoc.perl.org/perlintro|perlintro];
in particular, the "[https://perldoc.perl.org/perlintro#Perl-variable-types|Perl variable types]" section.
At the end of that section you'll find the gentlest of introductions to references
with a list of other links to more information;
I'd suggest checking out [https://perldoc.perl.org/perlreftut|perlreftut] first.
</p>
<p>
Consider the following:
</p>
<code>
$ perl -E '
my $x = 5;
say $x;
my $y = \$x;
say $y;
say $$y;
$$y += 3;
say $x;
'
5
SCALAR(0x60008a1c8)
5
8
</code>
<ul>
<li>
<c>$x</c> has the value <c>5</c>.
</li>
<li>
<c>$y</c> is assigned a reference to <c>$x</c>.
</li>
<li>
<c>$y</c> has the value <c>SCALAR(0x60008a1c8)</c>.
</li>
<li>
Dereferencing <c>$y</c> (with <c>$$y</c>) gives you back <c>$x</c>.
</li>
<li>
Incrementing <c>$$y</c> by <c>3</c> is the same as incrementing <c>$x</c> by <c>3</c>.
</li>
<li>
<c>$x</c> now has the value <c>8</c>.
</li>
</ul>
<p>
You can reference and dereference to great depths if you want; as in this exaggerated example:
</p>
<code>
$ perl -E 'my $x = 5; say $x; my $y = \\\\$x; $$$$$y += 3; say $x'
5
8
</code>
<p>
The construct <c>\(...)</c>, where <c>...</c> is some list, evaluates to a list of references to each element of the list:
</p>
<code>
$ perl -E 'say for \(qw{1 2 3})'
SCALAR(0x60008a730)
SCALAR(0x60008a7d8)
SCALAR(0x60008a748)
</code>
<p>
You can take references to other data types:
</p>
<code>
$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \@x; say $y'
1
2
3
ARRAY(0x60008a8e8)
</code>
<p>and dereference them:</p>
<code>
$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \@x; say $y; say for @$y'
1
2
3
ARRAY(0x60008a828)
1
2
3
</code>
<p>
You can take references to references:
</p>
<code>
$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \\@x; say $y'
1
2
3
REF(0x600003e80)
</code>
<p>
and dereference them one level at a time:
</p>
<code>
$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \\@x; say $y; say $$y; say for @$$y'
1
2
3
REF(0x600003e80)
ARRAY(0x60008a868)
1
2
3
</code>
<p>
I suggest you play around with examples like these to get a better understanding of how all of this works.
</p>
<p>
Also note that I didn't use, or indeed need, terms such as <em>pointer</em>, <em>address</em> or <em>memory location</em>.
</p>
<p>
You used <c>strict</c> and <c>warnings</c> in your OP which is very good.
I suggest you do the same with oneliners.
Here's a common <c>alias</c> I use; you might want to set up something similar for yourself
(although, perhaps, one a little less involved).
</p>
<code>
$ alias perle
alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E'
</code>
<p>
That will pick up things like this:
</p>
<code>
$ perl -E '$x =5'
$ perle '$x =5'
Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) ...
$ perl -E 'my $x = 5; say @$x'
$ perle 'my $x = 5; say @$x'
Can't use string ("5") as an ARRAY ref while "strict refs" in use ...
</code>
<p></p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-861371">
<p>— Ken</p>
</div></div>
11124042
11124042