### comment on

 Need Help??

Fellow Monks,

I am still trying to wrap my head around pointers/references in Perl. I came up with the following program to help me better understand but was hoping you could please help me with some questions.

```use warnings;
use strict;
use v5.10;

my \$variable = 22;
my \$pointer = \\$variable;

say "The address of \\$varible, which contains the value \$variable,";
say "is \$pointer";

\$\$pointer = 25;

say "Look at that!  \\$variable now equals \$variable";

sub sum_and_diff {
my \$a = shift @_;
my \$b = shift @_;
my \$res = \(shift @_); # why does the "\" work here?
my \$sum = \$a + \$b;
\$\$res = \$a - \$b;
return \$sum;
}

my \$b = 2;
my \$diff;  # this is line 27
my \$pointer_to_diff = \\$diff;

say "the sum of 5 and \$b is ", &sum_and_diff(5, \$b, \$pointer_to_diff);
say "and the difference is ", \$pointer_to_diff;

say "the sum of 9 and \$b is ", &sum_and_diff(9, \$b, \\$diff);
say "and the difference is ", \$diff;  # this is line 34

(1) Does the backslash ("\") in the my \$res line mean that \$res contains the address of the third argument passed to the function? I think so but just wanted to confirm.

(2) Does the "double dollar sign" ("\$\$") two lines later mean to put the value of the difference of \$a and \$b in the memory location that is \$res?

(3) Why do the lines using \$pointer_to_diff work but the last two lines using \\$diff and \$diff not work? I thought that these lines were essentially equivalent and that \$diff was defined in line 27. Instead, I get the following output:

```The address of \$varible, which contains the value 22,
is SCALAR(0x801e64540)
Look at that!  \$variable now equals 25
the sum of 5 and 2 is 7
and the difference is 3
the sum of 9 and 2 is 11
Use of uninitialized value \$diff in say at line 34.
and the difference is

Gratias tibi ago
Leudwinus

Edited to add: I thought using \${\\$diff} in the last line would work but that too gave me the same error.

In reply to Pointers and References by Leudwinus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or or How to display code and escape characters are good places to start.

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2021-06-18 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What does the "s" stand for in "perls"? (Whence perls)

Results (90 votes). Check out past polls.

Notices?