4 if( $ENV{PERL_CORE} ) {
10 use Test::More tests => 74;
12 my $ro_err = qr/^Modification of a read-only value attempted/;
17 ok( !Internals::SvREADONLY $foo );
21 ok( Internals::SvREADONLY $foo, 1 );
22 ok( Internals::SvREADONLY $foo );
23 eval { $foo = 'foo'; };
24 like($@, $ro_err, q/Can't modify read-only scalar/);
25 eval { undef($foo); };
26 like($@, $ro_err, q/Can't undef read-only scalar/);
29 ok( !Internals::SvREADONLY $foo, 0 );
30 ok( !Internals::SvREADONLY $foo );
37 ok( !Internals::SvREADONLY @foo );
42 ok( Internals::SvREADONLY @foo, 1 );
43 ok( Internals::SvREADONLY @foo );
44 eval { undef(@foo); };
45 like($@, $ro_err, q/Can't undef read-only array/);
46 eval { delete($foo[2]); };
47 like($@, $ro_err, q/Can't delete from read-only array/);
48 eval { shift(@foo); };
49 like($@, $ro_err, q/Can't shift read-only array/);
50 eval { push(@foo, 'bork'); };
51 like($@, $ro_err, q/Can't push onto read-only array/);
52 eval { @foo = qw/foo bar/; };
53 like($@, $ro_err, q/Can't reassign read-only array/);
55 ok( !Internals::SvREADONLY @foo, 0 );
56 ok( !Internals::SvREADONLY @foo );
57 eval { @foo = qw/foo bar/; };
61 ### Read-only array element
63 ok( !Internals::SvREADONLY $foo[2] );
67 ok( Internals::SvREADONLY $foo[2], 1 );
68 ok( Internals::SvREADONLY $foo[2] );
71 is($foo[0], 99, 'Rest of array still modifiable');
74 ok( Internals::SvREADONLY $foo[1] );
75 eval { $foo[1] = 'bork'; };
76 like($@, $ro_err, 'Read-only array element moved');
79 ok( !Internals::SvREADONLY $foo[2] );
84 ok( !Internals::SvREADONLY $foo[1] );
85 ok( Internals::SvREADONLY $foo[2] );
87 eval { $foo[2] = 86; };
88 like($@, $ro_err, q/Can't modify read-only array element/);
89 eval { undef($foo[2]); };
90 like($@, $ro_err, q/Can't undef read-only array element/);
92 local $TODO = 'Due to restricted hashes implementation';
93 eval { delete($foo[2]); };
94 like($@, $ro_err, q/Can't delete read-only array element/);
97 ok( !Internals::SvREADONLY $foo[2], 0 );
98 ok( !Internals::SvREADONLY $foo[2] );
100 is($foo[2], 'xyzzy');
105 ok( !Internals::SvREADONLY %foo );
106 %foo = ('foo' => 1, 2 => 'bar');
107 is(scalar(keys(%foo)), 2);
110 ok( Internals::SvREADONLY %foo, 1 );
111 ok( Internals::SvREADONLY %foo );
112 eval { undef(%foo); };
113 like($@, $ro_err, q/Can't undef read-only hash/);
115 local $TODO = 'Due to restricted hashes implementation';
116 eval { %foo = ('ping' => 'pong'); };
117 like($@, $ro_err, q/Can't modify read-only hash/);
119 eval { $foo{'baz'} = 123; };
120 like($@, qr/Attempt to access disallowed key/, q/Can't add to a read-only hash/);
122 # These ops are allow for Hash::Util functionality
124 is($foo{2}, 'qux', 'Can modify elements in a read-only hash');
125 my $qux = delete($foo{2});
126 ok(! exists($foo{2}), 'Can delete keys from a read-only hash');
129 is($foo{2}, 2, 'Can add back deleted keys in a read-only hash');
131 ok( !Internals::SvREADONLY %foo, 0 );
132 ok( !Internals::SvREADONLY %foo );
134 ### Read-only hash values
136 ok( !Internals::SvREADONLY $foo{foo} );
138 is($foo{'foo'}, 'bar');
140 ok( Internals::SvREADONLY $foo{foo}, 1 );
141 ok( Internals::SvREADONLY $foo{foo} );
142 eval { $foo{'foo'} = 88; };
143 like($@, $ro_err, q/Can't modify a read-only hash value/);
144 eval { undef($foo{'foo'}); };
145 like($@, $ro_err, q/Can't undef a read-only hash value/);
146 my $bar = delete($foo{'foo'});
147 ok(! exists($foo{'foo'}), 'Can delete a read-only hash value');
150 ok( !Internals::SvREADONLY $foo{foo}, 0 );
151 ok( !Internals::SvREADONLY $foo{foo} );
153 is( Internals::SvREFCNT($foo), 1 );
156 is( Internals::SvREFCNT($foo), 2 );
157 is( Internals::SvREFCNT($bar), 1 );
159 is( Internals::SvREFCNT($foo), 1 );
161 is( Internals::SvREFCNT(@foo), 1 );
162 is( Internals::SvREFCNT($foo[2]), 1 );
163 is( Internals::SvREFCNT(%foo), 1 );
164 is( Internals::SvREFCNT($foo{foo}), 1 );