Kyle
Kyle

Reputation: 4597

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!

Upvotes: 6

Views: 1107

Answers (1)

amon
amon

Reputation: 57650

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.

Upvotes: 14

Related Questions