I'm trying to return an anonymous array from a subroutine, however, when dumping the returned variable I only see one value (I'm expecting two).
Here is my code:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $fruits_ref = generate_fruits();
print "Fruits: " . Dumper($fruits_ref) . "\n";
sub generate_fruits
{
return ("Apple", "Orange");
}
This outputs:
Fruits: $VAR1 = 'Orange';
How do I get the subroutine to return that array ref?
["Apple", "Orange"]$fruits_ref = [ generate_fruits() ]my @fruits = generate_fruits();. But that is likely not what you need - you seem to need a reference. For that, just use square brackets. Oh, another alternative ismy $fruits_ref = [generate_fruits()];