4

In Perl, if I create two references to an array element, the two pointers are equal.

my $ref1 = \$array[0];
my $ref2 = \$array[0];
print "$ref1\n$ref2";

The same applies to two references to a variable which stores a string, those pointers are equal.

If I create two variables in which I store equal strings, the references are not equal.

Why aren't they equal? The same data is stored in different places?

Does Perl refer to the variable instead of the memory location?

In Java, as you can see here, four equal strings refer to the same memory location.

Can somebody please explain that?

Community
  • 1
  • 1
John Doe
  • 1,058
  • 8
  • 30

1 Answers1

7

If you take several reference of one variable, all of them will point to the same memory location.

my $foo = 'foo';
my $ref1 = \$foo;
my $ref2 = \$foo;

say $ref1;
say $ref2;

The value behind $ref1 and $ref2 is the same, because they both point to the same variable.

SCALAR(0x171f7b0)
SCALAR(0x171f7b0)

If you assign the string (not the same variable containing a string) to two new variables and then take references for those, they will be different.

my $foo = 'foo';
my $bar = 'bar';
my $ref1 = \$foo;
my $ref2 = \$bar;

say $ref1;
say $ref2;

Here, $ref1 and $ref2 are not the same, because they are references to two different variables.

SCALAR(0x20947b0)
SCALAR(0x2094678)

The fact that both variables hold an equal value doesn't matter.

my $ref1 = \'foo';
my $ref2 = \'foo';

say $ref1;
say $ref2;

The same goes for if you directly take references to values without putting them into another variable first.

SCALAR(0x1322528)
SCALAR(0x12ee6f0)

Perl handles the memory internally, but it's not like it has a table with every possible string, and whenever you create a reference, it just uses that memory address.

In Java, strings are objects. Java knows about the objects that are available to it. When you define a string, it makes an object. That is not the case in Perl. Strings and numbers are just values, and they are put into memory when you use them.

What Perl does is keep track of its references. It will free up memory by removing unused values, but only if there are no more references to those values living somewhere else in the running program. That's called the ref count.

You can read about references in perlref. There also is a pretty good explanation of how all of this works in the book Object Oriented Perl by Damian Conway at Mannings.

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • 1
    *"Object Oriented Perl"* is pretty good, but it hasn't been updated since 1999. Perl first became *object-oriented* with Perl 5.0 only in 1994; Damian has always been at the cutting edge of Perl, and has been working on Perl 6 for several years. I think the best writing on object-oriented Perl is [Modern Perl](http://modernperlbooks.com/). So far the latest edition has always been available on line; the fourth is [here in HTML](http://modernperlbooks.com/books/modern_perl_2016/index.html). – Borodin Aug 12 '16 at 09:47
  • @Borodin I agree, but I mentioned it here specifically for the chapter on references. That part is still very current. – simbabque Aug 12 '16 at 09:52
  • Yes, of course. I recommend pretty much anything that Damian Conway has written. And Perl 6 is marvellous: by far the best language I have ever seen. – Borodin Aug 12 '16 at 10:10