3 # Copyright 2002, Larry Wall.
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
13 if ($Config::Config{'extensions'} !~ /\bStorable\b/) {
14 print "1..0 # Skip: Storable was not built\n";
19 print "1..0 # Skip: No Hash::Util pre 5.005\n";
21 # And doing this seems on 5.004 seems to create bogus warnings about
22 # unitialized variables, or coredumps in Perl_pp_padsv
23 } elsif (!eval "require Hash::Util") {
24 if ($@ =~ /Can\'t locate Hash\/Util\.pm in \@INC/s) {
25 print "1..0 # Skip: No Hash::Util:\n";
37 use Storable qw(dclone freeze thaw);
38 use Hash::Util qw(lock_hash unlock_value);
42 my %hash = (question => '?', answer => 42, extra => 'junk', undef => undef);
44 unlock_value %hash, 'answer';
45 unlock_value %hash, 'extra';
46 delete $hash{'extra'};
50 package Restrict_Test;
53 return (undef, $_[0]);
59 my $temp = freeze $_[0];
66 my $copy = &$cloner($hash);
68 my @in_keys = sort keys %$hash;
69 my @out_keys = sort keys %$copy;
70 unless (ok ++$test, "@in_keys" eq "@out_keys") {
71 print "# Failed: keys mis-match after deep clone.\n";
72 print "# Original keys: @in_keys\n";
73 print "# Copy's keys: @out_keys\n";
76 # $copy = $hash; # used in initial debug of the tests
78 ok ++$test, Internals::SvREADONLY(%$copy), "cloned hash restricted?";
80 ok ++$test, Internals::SvREADONLY($copy->{question}),
81 "key 'question' not locked in copy?";
83 ok ++$test, !Internals::SvREADONLY($copy->{answer}),
84 "key 'answer' not locked in copy?";
86 eval { $copy->{extra} = 15 } ;
87 unless (ok ++$test, !$@, "Can assign to reserved key 'extra'?") {
90 print "# \$\@: $diag\n";
93 eval { $copy->{nono} = 7 } ;
94 ok ++$test, $@, "Can not assign to invalid key 'nono'?";
96 ok ++$test, exists $copy->{undef},
99 ok ++$test, !defined $copy->{undef},
100 "value for key 'undef' is undefined";
103 for $Storable::canonical (0, 1) {
104 for my $cloner (\&dclone, \&freeze_thaw) {
105 print "# \$Storable::canonical = $Storable::canonical\n";
106 testit (\%hash, $cloner);
108 # bless {}, "Restrict_Test";
111 $hash2{"k$_"} = "v$_" for 0..16;
114 unlock_value %hash2, "k$_";
115 delete $hash2{"k$_"};
117 my $copy = &$cloner(\%hash2);
121 eval { $copy->{$k} = undef } ;
122 unless (ok ++$test, !$@, "Can assign to reserved key '$k'?") {
124 $diag =~ s/\n.*\z//s;
125 print "# \$\@: $diag\n";