Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Reference to a function return (http://www.velocityreviews.com/forums/t913061-reference-to-a-function-return.html)

yoxoman 05-25-2010 07:02 PM

Reference to a function return
 
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.

John Bokma 05-25-2010 07:53 PM

Re: Reference to a function return
 
yoxoman <invalid@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

C.DeRykus 05-26-2010 11:17 AM

Re: Reference to a function return
 
On May 25, 12:02*pm, yoxoman <inva...@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


All times are GMT. The time now is 05:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.