diff options
author | Nicholas Clark <nick@ccl4.org> | 2002-04-14 00:43:02 +0100 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2002-04-14 15:20:58 +0000 |
commit | dfd4ef2f849f6c6c1ef68fdf03041001be25ade9 (patch) | |
tree | 95f2f7df3a87406f4db838e4719e3336e4cdd354 /lib/Hash | |
parent | fd96152af2273c1b6313770ef4d2d59bd42b8407 (diff) | |
download | perl-dfd4ef2f849f6c6c1ef68fdf03041001be25ade9.tar.gz |
Re: restricted hashes are unblessable
Message-ID: <20020413224302.GB14889@Bagpuss.unfortu.net>
The function name sucks but can't think of anything better.
p4raw-id: //depot/perl@15914
Diffstat (limited to 'lib/Hash')
-rw-r--r-- | lib/Hash/Util.pm | 1 | ||||
-rw-r--r-- | lib/Hash/Util.t | 37 |
2 files changed, 37 insertions, 1 deletions
diff --git a/lib/Hash/Util.pm b/lib/Hash/Util.pm index 528711ae55..a1c9e64a54 100644 --- a/lib/Hash/Util.pm +++ b/lib/Hash/Util.pm @@ -71,6 +71,7 @@ Removes the restriction on the %hash's keyset. sub lock_keys (\%;@) { my($hash, @keys) = @_; + Internals::hv_clear_placeholders %$hash; if( @keys ) { my %keys = map { ($_ => 1) } @keys; my %original_keys = map { ($_ => 1) } keys %$hash; diff --git a/lib/Hash/Util.t b/lib/Hash/Util.t index 1046e32c54..a42a52e03c 100644 --- a/lib/Hash/Util.t +++ b/lib/Hash/Util.t @@ -6,7 +6,7 @@ BEGIN { chdir 't'; } } -use Test::More tests => 45; +use Test::More tests => 55; my @Exported_Funcs; BEGIN { @@ -168,3 +168,38 @@ TODO: { lock_keys(%ENV); eval { () = $ENV{I_DONT_EXIST} }; like( $@, qr/^Attempt to access disallowed key 'I_DONT_EXIST' in a restricted hash/, 'locked %ENV'); + +{ + my %hash; + + lock_keys(%hash, 'first'); + + is (scalar keys %hash, 0, "place holder isn't a key"); + $hash{first} = 1; + is (scalar keys %hash, 1, "we now have a key"); + delete $hash{first}; + is (scalar keys %hash, 0, "now no key"); + + unlock_keys(%hash); + + $hash{interregnum} = 1.5; + is (scalar keys %hash, 1, "key again"); + delete $hash{interregnum}; + is (scalar keys %hash, 0, "no key again"); + + lock_keys(%hash, 'second'); + + is (scalar keys %hash, 0, "place holder isn't a key"); + + eval {$hash{zeroeth} = 0}; + like ($@, + qr/^Attempt to access disallowed key 'zeroeth' in a restricted hash/, + 'locked key never mentioned before should fail'); + eval {$hash{first} = -1}; + like ($@, + qr/^Attempt to access disallowed key 'first' in a restricted hash/, + 'previously locked place holders should also fail'); + is (scalar keys %hash, 0, "and therefore there are no keys"); + $hash{second} = 1; + is (scalar keys %hash, 1, "we now have just one key"); +} |