Perl uses a method called reference counting to ensure that memory stays allocated as long as there is at least one reference pointing to it. The code you've showed is fine. On every call of the sub, a new array will be allocated, and the reference pointing to it will keep it alive, even after the sub ends.
sub foo {
my $m = shift;
my @array = ($m*1, $m*2, $m*3);
return \@array;
}
my $x = foo(2);
my $y = foo(3);
use Data::Dump;
dd $x; # [2, 4, 6]
dd $y; # [3, 6, 9]
$x = undef; # *now* the memory for that array is freed
Update: For more details, see perlref.