Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference the return value of a perl sub

The code:

my $compare = List::Compare->new(\@hand, \@new_hand);
print_cards("Discarded", $compare->get_Lonly()) if ($verbose);

print_cards expects (scalar, reference to array).
get_Lonly returns array. What's the syntax to convert that to a reference so I can pass it to print_cards? \@{$compare->getLonly()} doesn't work, for example.

Thanks!

like image 559
Kyle Avatar asked Jul 10 '13 17:07

Kyle


1 Answers

You probably want

print_cards("Discarded", [$compare->get_Lonly])

Subroutines don't return arrays, they return a list of values. We can create an array reference with [...].

The other variant would be to make an explicit array

if ($verbose) {
  my @array = $compare->get_Lonly;
  print_cards("Discarded", \@array)
}

The first solution is a shortcut of this.


The @{ ... } is a dereference operator. It expects an array reference. This doesn't work as you think if you give it a list.

like image 144
amon Avatar answered Oct 13 '22 12:10

amon