Mark Dominus on 25 Nov 2003 04:08:19 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Why can't an array element be used as the index var in foreach?


Mark Rogaski <wendigo@pobox.com>:
> An entity claiming to be Walt Mankowski (waltman@pobox.com) wrote:
> :=20
> : In nearly every other case I can think of in Perl, array elements work
> : just like scalars.  You can, for instance, take references to them and
> : use local on them.  In fact, a reference to an array element says that
> : it's a scalar reference.
> :=20
> : So what's so special about foreach loops that array elements aren't
> : permitted?  And if they are explicitly forbidden, where is that
> : documented?
> :=20
> 
> I think the limitation has to do with foreach loop index being an alias,
> as opposed to a hard reference.  Being an alias, such a syntax would imply
> something like:
> 
>  *c[0] = 0;
> 
> Setting up such a glob would blow up when you tried to see what was in
> $c[0], because it wouldn't know whether it was a scalar named ${c[0]} or
> an array element named @{c[0]}.

I don't think that argument makes sense, because I think if it did you
would be able to make the same argument that

        foreach $c (...)

couldn't work.  I'm not sure, because I don't understand the
argument.  (That's the other reason I think it might not make sense.)

I think the correct answer to the question is that there's no reason
in principle why Walt's idea doesn't work, and that it could be made
to work without much difficulty, but nobody has ever bothered.

-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**