Reference to a function return

Y

yoxoman

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.
 
J

John Bokma

yoxoman said:
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() ];
 
C

C.DeRykus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Code help please 4
How do I get address of scalars? 1
Uncaught Reference Errors 1
Comparing a reference? 2
thread problem 1
Recognizing a regex reference 5
Constructing a scalar reference 4
Return confusion 4

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top