Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make lock_hash_recurse() unlock_hash_recurse() exportable;

include them in SYNOPSIS; write tests for them.
  • Loading branch information...
commit f32dcc743b845b58cef4760560b129fcb6530092 1 parent cbd4469
@jkeenan authored
Showing with 33 additions and 6 deletions.
  1. +7 −5 lib/Hash/Util.pm
  2. +26 −1 t/Util.t
View
12 lib/Hash/Util.pm
@@ -29,6 +29,7 @@ our @EXPORT_OK = qw(
hidden_ref_keys legal_ref_keys
hash_seed hv_store
+ lock_hash_recurse unlock_hash_recurse
);
our $VERSION = '0.12';
require XSLoader;
@@ -72,6 +73,7 @@ Hash::Util - A selection of general-utility hash subroutines
hidden_ref_keys legal_ref_keys
hash_seed hv_store
+ lock_hash_recurse unlock_hash_recurse
);
%hash = (foo => 42, bar => 23);
@@ -142,8 +144,8 @@ the hash before you call lock_keys() so this shouldn't be a problem.
Removes the restriction on the %hash's keyset.
-B<Note> that if any of the values of the hash have been locked they will not be unlocked
-after this sub executes.
+B<Note> that if any of the values of the hash have been locked they will not
+be unlocked after this sub executes.
Both routines return a reference to the hash operated on.
@@ -314,9 +316,9 @@ lock_hash() locks an entire hash and any hashes it references recursively,
making all keys and values read-only. No value can be changed, no keys can
be added or deleted.
-B<Only> recurses into hashes that are referenced by another hash. Thus a
-Hash of Hashes (HoH) will all be restricted, but a Hash of Arrays of Hashes
-(HoAoH) will only have the top hash restricted.
+This method B<only> recurses into hashes that are referenced by another hash.
+Thus a Hash of Hashes (HoH) will all be restricted, but a Hash of Arrays of
+Hashes (HoAoH) will only have the top hash restricted.
unlock_hash_recurse(%hash);
View
27 t/Util.t
@@ -23,9 +23,10 @@ BEGIN {
hidden_ref_keys legal_ref_keys
hash_seed hv_store
+ lock_hash_recurse unlock_hash_recurse
);
# plan tests => 208 + @Exported_Funcs;
- plan tests => 219 + @Exported_Funcs;
+ plan tests => 225 + @Exported_Funcs;
use_ok 'Hash::Util', @Exported_Funcs;
}
foreach my $func (@Exported_Funcs) {
@@ -496,3 +497,27 @@ ok($hash_seed >= 0, "hash_seed $hash_seed");
is_deeply(\@ph, \@bam, "Placeholders in place");
}
+{
+ my %hash = (
+ a => 'alpha',
+ b => [ qw( beta gamma delta ) ],
+ c => [ 'epsilon', { zeta => 'eta' }, ],
+ d => { theta => 'iota' },
+ );
+ lock_hash_recurse(%hash);
+ ok( hash_locked(%hash),
+ "lock_hash_recurse(): top-level hash locked" );
+ ok( hash_locked(%{$hash{d}}),
+ "lock_hash_recurse(): element which is hashref locked" );
+ ok( ! hash_locked(%{$hash{c}[1]}),
+ "lock_hash_recurse(): element which is hashref in array ref not locked" );
+
+ unlock_hash_recurse(%hash);
+ ok( hash_unlocked(%hash),
+ "unlock_hash_recurse(): top-level hash unlocked" );
+ ok( hash_unlocked(%{$hash{d}}),
+ "unlock_hash_recurse(): element which is hashref unlocked" );
+ ok( hash_unlocked(%{$hash{c}[1]}),
+ "unlock_hash_recurse(): element which is hashref in array ref not locked" );
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.