This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test preamble: unify chdir 't' if -d 't';
[perl5.git] / t / lib / universal.t
CommitLineData
80b6a949
AB
1#!./perl
2
3# Test the Internal::* functions and other tibits in universal.c
4
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8 require './test.pl';
57c404c9 9 plan( tests => 16 );
80b6a949
AB
10}
11
12for my $arg ('', 'q[]', qw( 1 undef )) {
20e5bab4 13 fresh_perl_is(<<"----", <<'====', {}, "Internals::* functions check their argument under func() AND &func() [perl #77776]");
80b6a949
AB
14sub tryit { eval shift or warn \$@ }
15tryit "&Internals::SvREADONLY($arg)";
16tryit "&Internals::SvREFCNT($arg)";
17tryit "&Internals::hv_clear_placeholders($arg)";
80b6a949
AB
18----
19Usage: Internals::SvREADONLY(SCALAR[, ON]) at (eval 1) line 1.
20Usage: Internals::SvREFCNT(SCALAR[, REFCOUNT]) at (eval 2) line 1.
21Usage: Internals::hv_clear_placeholders(hv) at (eval 3) line 1.
80b6a949
AB
22====
23}
21690b72
FC
24
25# Various conundrums with SvREADONLY
26
27$x = *foo;
28Internals::SvREADONLY $x, 1;
3e89ba19
FC
29ok Internals::SvREADONLY($x),
30 'read-only glob copies are read-only acc. to Internals::';
21690b72
FC
31eval { $x = [] };
32like $@, qr/Modification of a read-only value attempted at/,
33 'read-only glob copies';
3e89ba19
FC
34Internals::SvREADONLY($x,0);
35$x = 42;
36is $x, 42, 'Internals::SvREADONLY can turn off readonliness on globs';
37
3a482d8d
FC
38# Same thing with regexps
39$x = ${qr//};
40Internals::SvREADONLY $x, 1;
41ok Internals::SvREADONLY($x),
42 'read-only regexps are read-only acc. to Internals::';
43eval { $x = [] };
44like $@, qr/Modification of a read-only value attempted at/,
45 'read-only regexps';
46Internals::SvREADONLY($x,0);
47$x = 42;
48is $x, 42, 'Internals::SvREADONLY can turn off readonliness on regexps';
49
3e89ba19
FC
50$h{a} = __PACKAGE__;
51Internals::SvREADONLY $h{a}, 1;
52eval { $h{a} = 3 };
53like $@, qr/Modification of a read-only value attempted at/,
54 'making a COW scalar into a read-only one';
55
56$h{b} = __PACKAGE__;
57ok !Internals::SvREADONLY($h{b}),
58 'cows are not read-only acc. to Internals::';
59Internals::SvREADONLY($h{b},0);
60$h{b} =~ y/ia/ao/;
61is __PACKAGE__, 'main',
62 'turning off a cow’s readonliness did not affect sharers of the same PV';
a623f893
FC
63
64&Internals::SvREADONLY(\!0, 0);
65eval { ${\!0} = 7 };
66like $@, qr "^Modification of a read-only value",
67 'protected values still croak on assignment after SvREADONLY(..., 0)';
68is ${\3} == 3, "1", 'attempt to modify failed';
57c404c9
FC
69
70eval { { my $x = ${qr//}; Internals::SvREADONLY $x, 1; () } };
71is $@, "", 'read-only lexical regexps on scope exit [perl #115254]';