http://www.perlmonks.org?node_id=248786


in reply to Surpised by foreach iterator limitation

Just to recap my main point thru out this subthread: (many points here is not directly against the original post, but against the whole subthread. Many of those things I disagree with is even not part of shotgunefx's original purpose, but MarkM's extension. I disagree with MarkM's points, but not him as a fellow monk ;-)

$hash{key} is the valid syntax to name a hash element, which happened to be a scalar (which could be array ref, hash ref ...), does not mean $hash{key} is a valid variable name for a scalar.

There is something subtle here, and we are looking at two different perspectives.

maybe a better way to explain this subtle difference is to look at some other computer language, for example c:

You can define a collective int a[200];, and ref to its 100th element by saying a[99], but a[99] is not a valid variable name. Otherwise, how can you recognize whether a[99] is a single variable or an element of a collective data structure?

One may say that this is Perl, not c. Well, when you look at the foundation, they have more similarities than differences. Whatever language it is, it requires the syntax can be consistently parsed/recognized.

Some people may argue that there are lots of "bad" syntax in Perl, what a big deal to have one more? Again, here is something subtle. Perl might have many "bad" syntax from a conventional language point of view, but all those "bad" syntax can be consistently parsed according to context. I bet nobody dare to have "bad" syntax that cannot be consistently parsed.

(the original content of this original reply is based on misunderstanding of what shotgunefx means. He actually means something much more subtle.)I don't call that a limitation ;-). To iterate thru a hash, you would do something like:
%hash = (a=>1, b=> 2, c=>3); foreach (keys %hash) { print "\$hash{$_} = $hash{$_}\n"; }
What makes this conventional approach not work for you?