Prev: FAQ 8.20 How can I call my system's unique C functions from Perl?
Next: FAQ 8.43 How do I open a file without blocking?
From: yoxoman on 25 May 2010 15:02 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 25 May 2010 15:24 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 25 May 2010 15:53 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 26 May 2010 07:17
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 |