in reply to enforcing list context

There is no need to do this recursively. In fact, that's the last thing you want to do, since each item requires an additional function call, and only a couple hundred simultaneous function calls are necessary to crash Perl. What you want is something more like:
use strict; use warnings; my $r = 0; $r = { rest => $r, first => 'c' }; $r = { rest => $r, first => 'b' }; $r = { rest => $r, first => 'a' }; print join " ", value($r), scalar value($r); sub value { my $self = $_[0]; if (wantarray) { my @value; while ($self) { push @value, $self->{'first'}; $self = $self->{'rest'}; } return @value; } my $c; while ($self) { $c++; $self = $self->{'rest'}; } return $c; }
It might be simpler to just have a master data item with a "length" value, however, since this would require constant time to retrieve rather than linear time, and would allow you to significantly simplify the above function.

Replies are listed 'Best First'.
Re^2: enforcing list context
by billh (Pilgrim) on Apr 26, 2006 at 20:38 UTC
    Yes, that's a definate option, but recursive solutions are often the most elegant to write.
    btw, I recently found a neat way around Perl's stack depth limits, it goes something like this:
    @_ = ($self->{rest}); goto &{ $self->can('value') };
    :-)
    update:
    sorry, that should have read
    goto &{ $_[0]->can('value') };
    -----
    perl -e 'print sub { "Hello @{[shift]}!\n" }->("World")'