At 11:59 AM 7/18/2001 -0400, darren chamberlain wrote:
>Dan Sugalski <[EMAIL PROTECTED]> said something to this effect on 07/18/2001:
> > That should be "anonymous array". There's no such thing as an
> > anonymous list in perl 5.
> > (There is in perl 6, though you can't take a reference to it)
>
>Can you elaborate on that?
Sure.
In perl 5, you have three basic datatypes under the hood--the scalar, the
array, and the hash. (Represented, generally, by SV *, AV*, and HV *
respectively, and we're skipping the glob here) Arrays and hashes both have
sets of scalars in them. Works reasonably well, until you try doing
something like:
sub foo {
my @bar;
$bar[time]=1;
return @bar;
}
@baz = foo();
Now, that's a contrived example, but it'll chew up an ungodly amount of
memory--first to create @bar, second to flatten every single element of
@bar onto the stack (thus blowing the stack up really big) and then to blow
@baz out as big as it needs to be. That's a horrid waste, especially when
most of the memory used (the stack and what's in @bar) is destined to be
trashed as soon as the copy's done.
Now, perl 6 will have four types of variables internally. In addition to
scalars, arrays, and hashes, we'll also have lists. Lists are mostly like
arrays with two big differences--they can hold arrays, hashes, and lists in
addition to scalars, and any array, hash, or list in them may be evaluated
lazily.
What does this mean for the above example? Well, @bar still gets huge. No
way around that. (Possibly less huge, if we do sparse arrays, but that's
neither here nor there) The return statement, though, rather than returning
a huge list of scalars, returns a single value--a list structure that has
one element in it, a pointer to the @bar array. Then, when we assign the
list to @baz, we have the distinct possibility of not actually needing to
do any copying at all--instead we can take the pointer to the underlying
array structure and just stick it in the slot for @baz. And even if we
can't (for example, because there's a reference to @bar somewhere) it
should be more efficient to do an array to array copy than it would be to
assign in a huge mass of scalars.
It also means that perl may be able to do:
(@foo, @bar) = baz();
and really have data go into both @foo and @bar--since we don't flatten, it
means array and hash elements don't lose their grouping on assignment or
subroutine returns, so we can figure out how to do this right. (Whether we
*will* or not is a separate issue, and that's Larry's problem. I'm just
going to make sure the mechanism is there if he chooses to expose it)
Dan
--------------------------------------"it's like this"-------------------
Dan Sugalski even samurai
[EMAIL PROTECTED] have teddy bears and even
teddy bears get drunk