each() is vulnerable. Like it or not, your re-written version is the standard idiom for looping through a hash's key-value pairs when *anything* outside your control could interfere with an each() call.
For related info, see the thread Spot the bug, especially the replies by merlyn and ysth.foreach my $key ( keys %hash ) { my $val = $hash{$key}; ... }
To improve readability with your long package names, you might do some combination of these refactorings:
foreach my $key ( keys %some::very::long::named::module::foo ) { my $val = $some::very::long::named::module::foo{$key};
{ local *foo = *some::very::long::named::module::foo; local *draw = *some::very::long::named::module::drawSomethingPretty; foreach my $key ( grep { testconditions($_) } keys %foo ) { drawSomethingPretty( $foo{$key} ); } }
This is the most "correct" way to handle the issue; it is exactly this issue that Exporter is designed to resolve. Work with the author to nail down this part of the module's API. If direct access to %foo is truely part of the API, then it should definitely be available for import. Otherwise, some accessor method should be provided, or the module docs should make clear a proper path to meet the same needs.use some::very::long::named::module qw( foo drawSomethingPretty ); foreach my $key ( keys %foo ) { next if not testconditions($key); drawSomethingPretty( $foo{$key} ); }
In reply to Re: Being impervious to an 'each' reset
by Util
in thread Being impervious to an 'each' reset
by throop
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |