My question is when and how variables in a routine free memory. The script is an example:
use strict;
sub A{
my $x= shift;
return ([$x]);
}
for my $i (1..10){
my $ref= &A($i);
my $ref2= &A(9);
print "$ref\t";
print "$ref2\n";
}
and the outputs on the screen:
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
ARRAY(0x996e98) ARRAY(0x9b50c8)
I expected links to be changed when subroutine A was called more than once, but the output links were fixed regardless of when the input was changed or not. Can a phenomenon detect that the memory occupied by a variable in routines can never be released until the whole script has finished? Otherwise, my result is unusual?
source
share