How do I turn the return value of a Perl sub into an arrayref?
Asked Answered
D

1

7

I try the code like this:

my @rows = getRows($sth);
$self->stash(rows => \@rows);

The getRows is a sub function name, and the code works in template. The $rows is a Array.

I write code like this:

$self->stash(rows => \getRows($sth));

The $rows is a REF, It's wrong.

If write code like this:

$self->stash(rows => getRows($sth));

The $rows is a HASH, It's wrong.

Is there any way to write the two line code in one?

Darky answered 5/8, 2012 at 0:53 Comment(0)
H
15

Yes. You can write

$self->stash(rows => [getRows($sth)]);

The square brackets [] serve to create the desired reference.

Hydrothermal answered 5/8, 2012 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.