In the following code upto what scope the anonymous array referred by $ref is available.
mod1.pm:
package mod1;
sub do_something{
.....
my $array_ref = ["elemnt1","elmnt2"] ;
return $array_ref ;
}
1;
file.pl
use mod1;
my $ref = mod1::do_something() ;
print "$ref->[0] $ref->[1] " ; #works
From the question it sounds like you are struggling with the difference between the scope of a variable, and the persistence of data pointed to by a reference. The data [“elemnt1″,”elmnt2”] is assigned to a variable ($array_ref) that goes out of scope at the end of do_something. However, because it is returned, there is a reference to the data, and it persists even when $array_ref goes out of scope.
Last time I checked, perl used reference-counting for garbage collection, so that array will not be garbage-collected until the last reference to it goes out of scope, e.g., if you unset $ref at the end of your example.