From: yoxoman on
Hello,

In the expressions

my $ref = \foo();

or

my $ref = \$myobj->foo

$ref is a reference to a scalar, even if the foo function returns an
array (in that case, $ref points to an element of array...)

Do you know why ?


Thanks.
From: Ben Morrow on

Quoth yoxoman <invalid(a)invalid.invalid>:
>
> In the expressions
>
> my $ref = \foo();
>
> or
>
> my $ref = \$myobj->foo
>
> $ref is a reference to a scalar, even if the foo function returns an
> array (in that case, $ref points to an element of array...)
>
> Do you know why ?

Taking a ref to a function/method call parses as \LIST, which means that
it calls the function in list context. Assigning that to a scalar puts
the \LIST operator in scalar context, so it returns a ref to the last
element in that list.

If you want an arrayref, you need

my $ref = [ foo () ];

Ben

From: John Bokma on
yoxoman <invalid(a)invalid.invalid> writes:

> Hello,
>
> In the expressions
>
> my $ref = \foo();
>
> or
>
> my $ref = \$myobj->foo
>
> $ref is a reference to a scalar, even if the foo function returns an
> array (in that case, $ref points to an element of array...)
>
> Do you know why ?

I guess you want:

my $ref_to_return = [ foo() ];

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: C.DeRykus on
On May 25, 12:02 pm, yoxoman <inva...(a)invalid.invalid> wrote:
> Hello,
>
> In the expressions
>
> my $ref = \foo();
>
> or
>
> my $ref = \$myobj->foo
>
> $ref is a reference to a scalar, even if the foo function returns an
> array (in that case, $ref points to an element of array...)
> Do you know why?

That's because the members of the array are
returned as a list. The LHS is a scalar so
the comma operator acts on that list and the
last member of the list gets assigned to the
scalar.

See: perldoc -q list
"What is the difference between a list
and an array?" ...

Since \ provides a list, you might want to
just use an array on the LHS:

my @refs = \$myobj->foo.


--
Charles DeRykus