A little poking around on Google turned up an indication that I'm not missing something. There may not be a way to do exactly what I'm trying to do according to a post on Perlmonks.org. So instead of making a reference to a sub-routine's return value, you have to make a reference to a copy of the return value.
Here is the code that demonstrates this:
#!/usr/bin/perl
#From a post on caffeinemakesmesleepy.blogspot.com
# Demonstrate how to take an array return from one function and pass a reference to it into another.
#[] around return_array function makes a reference to a copy of the return value of that function.
# you need the () after return_array or "return_array" gets passed into take_array_ref
take_array_ref([return_array()]);
sub return_array {
my @ret;
push (@ret, 'FOO');
push (@ret, 'BAR');
return @ret;
}
sub take_array_ref {
my $ref = shift;
my $i = 0;
foreach my $value (@$ref) {
$i++;
print "Element #$i: $value\n";
}
}
No comments:
Post a Comment