Austin Hastings wrote:

--- Damian Conway <[EMAIL PROTECTED]> wrote:

	my @a is default(666);

	print @a[2];	# prints 666

	@a[4] = 1;

	print @a[2];	# now prints undef :-(
[typo in third line corrected]


I don't understand your example. Can you explain it again, using words
of less than one syllable?
If the scheme is (as someone was advocating) that the default is used only in place of elements that don't exist (as opposed to elements that are allocated but contain C<undef>), then allocating a previously unallocated element has the potential to change the value it returns, even if that previously unallocated element is not initialized when allocated.

So, assigning to @a[4], causes @a[0..3] to be created as well. Which,
under a "default-for-non-existent-elements-only" policy causes @a[2] to stop
returning the default.


What's @[4]? Why does it (even if it's a typo from @a[4]) cause @a[2]
to become defined?
It doesn't. It causes it to exist. I understood that it was being advocating
that only non-existent elements returned the default. This is why that's
a bad idea.


If it does become defined, why doesn't it get the default value --
> no-one explicitly told it to be undef?

Because the whole idea of a default value is that you *don't* put it
in every element. The lack of a value in an element is what triggers
the default value to be returned. And in Perl C<undef> is how we signify
the absence of a value.

Damian

Reply via email to