note
Leudwinus
<p>Hi Ken,</p>
<blockquote><i>You're using terms — pointer, address, memory location — 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.</i></blockquote>
<p>Guilty as charged! I was trying to replicate in Perl the following program from a C tutorial on functions and pointers:</p>
<code>
#include <stdio.h>
int sum_and_diff (int a, int b, int *res)
{
int sum;
sum = a + b;
*res = a – b;
return sum;
}
void main (void)
{
int b = 2;
int diff;
printf ("The sum of 5 and %d is %d\n", b,
sum_and_diff (5, b, &diff));
printf ("The difference of 5 and %d is %d\n", b, diff);
}
</code>
<p>Perhaps I just need to focus on one language at a time! And thank you for the detailed example and explanation!</p>
<blockquote><i>You can reference and dereference to great depths if you want; as in this exaggerated example</i></blockquote>
<p>I'm just reading this now but came to the [id://11124199|same conclusion] earlier today when I was reading some of the other responses to this thread.</p>
<blockquote><i>I suggest you play around with examples like these to get a better understanding of how all of this works.</i></blockquote>
<p>Agreed. What I take away from your examples is that when I try to print or output a variable that contains a reference, I can clearly see what that is in reference to. For example:</p>
<code>
SCALAR(0x60008a730) --> reference to a scalar
ARRAY(0x60008a828) --> reference to an array
REF(0x600003e80) --> reference to a reference
</code>
<p>This helps you understand how to dereference it:</p>
<code>
$$x # dereference reference to scalar (if $x is a scalar)
@$x # dereference reference to array (if $x is reference to an array)
@$$x # dereference reference to reference of an array
</code>
<p>I hope I got that last bit right!</p>
<blockquote><i>Also note that I didn't use, or indeed need, terms such as pointer, address or memory location.</i></blockquote>
<p>Duly noted! And thanks for the <code>alias</code> tip. I will have to park that one away for the time being because as useful as Perl one-liners are, I don't think I'm quite ready to use them that frequently.</p>
<p>Gratias tibi ago
<br>
<i>Leudwinus</i></p>
11124042
11124049