#!/usr/bin/perl
A();
B();
sub A {
my @array = qw(value_1 value_2);
$array_ref = \@array;
}
sub B {
foreach my $i ( @{$array_ref} ) {
print "Array Value: $i \n";
}
}
Since the array is declared using the ‘my’ keyword, could the array reference be lost ?
Can any one brief me over this.
No, the scope of the variable expires, but not the memory address. The data will remain.
Isn’t this something you could have simply tried? =) I just copy/pasted your code and tried it, and it worked fine.
For proper encapsulation, though, you really should return the array ref instead: