My question is about how Perl manages the data of objects internally.
When creating objects in Perl, the new subroutine will typically return a reference to a blessed object.
Take the following code as an example:
# Create a new object
my $object = Object->new(%data1);
# Create a new object with the same variable
$object = Object->new(%data2);
From the first call to new
we create an $object
that references some blessed %data1
Visual representation:
The "\" symbolizes a reference.
$object ════> \{bless %data1}
This would look as follows in memory:
The "&" symbolizes an address
MEMORY:
----------------------------------
&{bless %data1} ════> bless %data1
Then with the second call to new
the value of $object
is changed to reference some other blessed %data2
Visual representation:
$object ══/ /══> \{bless %data1} # The connection to %data1 is broken
║
╚═══════════> \{bless %data2}
Now memory would look like this:
MEMORY:
----------------------------------
&{bless %data1} ════> bless %data1
&{bless %data2} ════> bless %data2
The problem is now that $object
no longer stores the reference \{bless %data1}
, the address &{bless %data1}
and any data stored at this address is lost forever. There is no possible way to access the data stored at that location from the script anymore.
My question is . . . Is Perl smart enough to remove the data stored in &{bless %data1}
once the reference to this data is lost forever, or will Perl keep that data in memory potentially causing a memory leak?