develooper Front page | perl.perl5.porters | Postings from March 2013

Re: Perlfunc for each(), keys(), values() has been changed

Thread Previous | Thread Next
March 30, 2013 16:58
Re: Perlfunc for each(), keys(), values() has been changed
Message ID:
On 29 March 2013 19:05, Uri Guttman <> wrote:
> On 03/29/2013 01:46 PM, demerphq wrote:
>> ps: Dont worry, i have no plan of making keys annoying to use, but if
>> I can I would like to make for (keys %hash) as fast as possible!
> what is wrong with?
>         while( my $key = each %hash )
> same effect as the optimized foreach loop other than using a named var
> (which i would do with foreach anyway).

The major objection to each() is the shared state of the iterator. I
rarely see people guard each() traversal with a reset using keys().

So making keys() as efficient to each() but without the dangers seems
like a good thing.

> now i usually do each with a key/val pair and then the empty list will be
> the boolean stop. the while $key needs a defined as '' or '0' are legal keys
> but false.

Good catch. Thanks

> still no reason to add an optimized for keys.

"make perl go faster" seems to me to be a pretty good reason all by itself. :-)


perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About