| 1 |
Hash-SafeKeys |
| 2 |
|
| 3 |
Every hash variable in Perl has its own internal iterator, |
| 4 |
accessed by the builtin C<each>, C<keys>, and C<values> |
| 5 |
functions. The iterator is also implicitly used whenever |
| 6 |
the hash is evaluated in list context. The iterator is |
| 7 |
"reset" whenever C<keys> or C<values> is called on a hash, |
| 8 |
including the implicit calls when the hash is evaluated in |
| 9 |
list context. That makes it dangerous to do certain hash |
| 10 |
operations inside a C<while ... each> loop: |
| 11 |
|
| 12 |
while (my($k,$v) = each %hash) { |
| 13 |
... |
| 14 |
@k = sort keys %hash; # Infinite loop! |
| 15 |
@v = grep { /foo/ }, values %hash; # Ack! |
| 16 |
print join ' ', %hash; # Run away! |
| 17 |
} |
| 18 |
|
| 19 |
C<Hash::SafeKeys> provides alternate functions to access |
| 20 |
the keys, values, or entire contents of a hash in a way |
| 21 |
that does not reset the iterator, making them safe to use |
| 22 |
in such contexts: |
| 23 |
|
| 24 |
while (my($k,$v) = each %hash) { |
| 25 |
... |
| 26 |
@k = sort safekeys %hash; # Can do |
| 27 |
@v = grep { /foo/ }, safevalues %hash; # No problem |
| 28 |
print join ' ', safecopy %hash; # Right away, sir |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
INSTALLATION |
| 33 |
|
| 34 |
To install this module, run the following commands: |
| 35 |
|
| 36 |
perl Makefile.PL |
| 37 |
make |
| 38 |
make test |
| 39 |
make install |
| 40 |
|
| 41 |
SUPPORT AND DOCUMENTATION |
| 42 |
|
| 43 |
After installing, you can find documentation for this module with the |
| 44 |
perldoc command. |
| 45 |
|
| 46 |
perldoc Hash::SafeKeys |
| 47 |
|
| 48 |
You can also look for information at: |
| 49 |
|
| 50 |
RT, CPAN's request tracker (report bugs here) |
| 51 |
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-SafeKeys |
| 52 |
|
| 53 |
AnnoCPAN, Annotated CPAN documentation |
| 54 |
http://annocpan.org/dist/Hash-SafeKeys |
| 55 |
|
| 56 |
CPAN Ratings |
| 57 |
http://cpanratings.perl.org/d/Hash-SafeKeys |
| 58 |
|
| 59 |
Search CPAN |
| 60 |
http://search.cpan.org/dist/Hash-SafeKeys/ |
| 61 |
|
| 62 |
|
| 63 |
LICENSE AND COPYRIGHT |
| 64 |
|
| 65 |
Copyright (C) 2012-2016 Marty O'Brien |
| 66 |
|
| 67 |
This program is free software; you can redistribute it and/or modify it |
| 68 |
under the terms of either: the GNU General Public License as published |
| 69 |
by the Free Software Foundation; or the Artistic License. |
| 70 |
|
| 71 |
See http://dev.perl.org/licenses/ for more information. |