http://qs1969.pair.com?node_id=73668


in reply to Dereference an array reference

I have an object where $self->things is an array.

I'm not sure I understand your intent, but $self->things is not an array. It might be a reference to an array, but it's not itself an array. Could that be your stumbling block?

# Andy Lester  http://www.petdance.com  AIM:petdance
%_=split';','.; Perl ;@;st a;m;ker;p;not;o;hac;t;her;y;ju';
print map $_{$_}, split //,
'andy@petdance.com'

Replies are listed 'Best First'.
Re: Re: Dereference an array reference
by araqnid (Beadle) on Apr 19, 2001 at 02:36 UTC
    actually, methods (and subs in general) can return arrays... but you're quite right that if it's returning an array REF, then it will be seen in this case as if it returned an array of 1 value, "ARRAY(xxxxxx)".
      actually, methods (and subs in general) can return arrays
      No, they can return lists in a list context, or scalars in a scalar context. Nothing else. Cannot return an array.

      -- Randal L. Schwartz, Perl hacker

        That's all well and good to say -- but it might help to give a little context (Oh my, what pun!) to ponder:

        #!/usr/bin/perl -w use strict; my $scalar; my @list; sub foo { return (42, 24, 10); } sub bar { my @array = (42, 24, 10); return @array; } $scalar = foo(); # same as: $scalar = (42,24,10); @list = foo(); # same as: @list = (42,24,10); print "$scalar:@list\n"; # prints: 10:42 24 10 $scalar = bar(); # same as: $scalar = @array; @list = bar(); # same as: @list = @array; print "$scalar:@list\n"; # prints: 3:42 24 10 __END__
        [methods (and subs in general)] can return lists in a list context, or scalars in a scalar context. Nothing else. Cannot return an array.

        Okay, merlyn. I'm puzzled again. If anyone else had posted this assertion, I would have made this as a correction rather than a genuine inquiry. The following snippet seems to demonstrate the ret_array sub returning an array. But I've been enough rounds on the array/list thing to know that things ain't always what they seem. Is more happening here than meets the eye?

        sub ret_array { return @_; } sub ret_list { return @_[0..$#_]; } my @array = ('a','b','c'); print scalar ret_array('a','b','c'); # 3 print scalar ret_array(@array); # 3 print scalar ret_list('a','b','c'); # c print scalar ret_list(@array); # c