 Think about Loose Coupling PerlMonks

### how do \$x = \12 differ from \$\$y = 12 ?

by borisz (Canon)
 on May 10, 2005 at 12:54 UTC Need Help??

borisz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm confused by refs. How do \$x and \$y in the following example differ? I supposed they are equal, but there must be a difference.
```my ( \$x, \$y );
\$x = \12;
\$\$y = 12;
[download]```
Boris

Replies are listed 'Best First'.
Re: how do \$x = \12 differ from \$\$y = 12 ?
by japhy (Canon) on May 10, 2005 at 12:58 UTC
There is a world of difference. Forget that you're using a constant (12) for now. Imagine this:
```my @data = ('a', 'b', 'c');
my (\$x, \$y);
\$x = \@data;
@\$y = @data;
[download]```
\$x refers to @data (that is, it holds a reference to @data). But \$y doesn't; rather, the array referenced in \$y holds a shallow copy of the elements in @data.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Thanks, that takes me closer to the answer. I wonder why \$y is undefined now.
```use Scalar::Util qw/weaken/;
my ( \$x, \$y );
\$x = \12;
\$\$y = 12;
weaken(\$x);
weaken(\$y);
print \$x;
print \$y;
__OUTPUT__
SCALAR(0x1807f24)
Use of uninitialized value in print at 5432.pl line 11.
[download]```
Boris
Someone else showed you Devel::Peek code that explains that. \$x has a refcount of 2, and \$y only has a refcount of 1. It's the same as doing:
```\$x = \@array;
\$y = [];
[download]```
There, \$x has a refcount of 2 (from \$x and @array), whereas \$y has a refcount of only 1.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: how do \$x = \12 differ from \$\$y = 12 ?
by gellyfish (Monsignor) on May 10, 2005 at 13:03 UTC

Well the two differ semantically in the first you are taking a reference to a scalar constant and in the second you are assigning a scalar constant to a dereference scalar ref. The first will be a readonly value. In these cases Devel::Peek is your friend:

```\$x:

SV = RV(0x8086d20) at 0x807b2e8
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
RV = 0x807b30c
SV = IV(0x807ae44) at 0x807b30c
REFCNT = 2
FLAGS = (IOK,READONLY,pIOK)
IV = 12

\$y:

SV = RV(0x8086d24) at 0x807b2f4
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
RV = 0x804c500
SV = IV(0x807ae4c) at 0x804c500
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 12
[download]```

/J\

Re: how do \$x = \12 differ from \$\$y = 12 ?
by ikegami (Pope) on May 10, 2005 at 14:08 UTC

When \$x and \$y aren't initialized, there is no functional difference. The difference is only noticeable when they've been previously initialized.

```my ( \$x, \$y );

\$x = \12;
\$\$y = 13;

my \$a = \$x;
my \$b = \$y;

\$x = \14;
\$\$y = 15;

print("a = \$\$a\n");  # 12
print("b = \$\$b\n");  # 15
print("x = \$\$x\n");  # 14
print("y = \$\$y\n");  # 15
[download]```

\$x = \14 create a reference to the constant 14. It then stores the reference in \$x, erasing \$x's previous content.

\$\$y = 15 replaces the value of the variable at which \$y points with 15. In my example, \$b points to that same variable. When \$y isn't initialized, a new variable is created through auto-vivification. In languages without auto-vivification (C, C++ and Java, for example), you get a NULL pointer error instead. In languages with auto-vivification, you just created a new variable, possibly by accident.

In C++ parlance, \$x = \14 is similar to

```x = new int;
*x = 14;
[download]```

while \$\$y = 15 is similar to

```if (y == NULL) {
y = new int;
}
*y = 15;
[download]```
Re: how do \$x = \12 differ from \$\$y = 12 ?
by Roy Johnson (Monsignor) on May 10, 2005 at 13:13 UTC
In the former, the value is already stored somewhere, and you are setting \$x to refer to that location. In the latter, the \$y already* refers to a location, and you are putting a value into it.

* in this case, the location is created and assigned Just In Time.

Caution: Contents may have been coded under pressure.
Re: how do \$x = \12 differ from \$\$y = 12 ?
by dragonchild (Archbishop) on May 10, 2005 at 12:57 UTC
They're references to two different locations. Think of them as pointers-with-safety and you'll be pretty close to reality.

• In general, if you think something isn't in Perl, try it out, because it usually is. :-)
• "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: how do \$x = \12 differ from \$\$y = 12 ?
by runrig (Abbot) on May 11, 2005 at 16:19 UTC
Here is one difference to think on:
```#!/usr/bin/perl

use strict;

my \$x = \12;
my \$\$y = 12;

\$\$y++;
\$\$x++;  # <<-line 9

#################
output:
Modification of a read-only value attempted at ./tst line 9.
[download]```
Update: gellyfish already mentioned this, though a demonstration is helpful :-)

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://455544]
Approved by bart
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

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

Results (95 votes). Check out past polls.

Notices?