readonly.t revision 1.1
1#!./perl
2
3BEGIN {
4    unless (-d 'blib') {
5	chdir 't' if -d 't';
6	@INC = '../lib';
7	require Config; import Config;
8	keys %Config; # Silence warning
9	if ($Config{extensions} !~ /\bList\/Util\b/) {
10	    print "1..0 # Skip: List::Util was not built\n";
11	    exit 0;
12	}
13    }
14}
15
16use Scalar::Util qw(readonly);
17use Test::More tests => 11;
18
19ok( readonly(1),	'number constant');
20
21my $var = 2;
22
23ok( !readonly($var),	'number variable');
24is( $var,	2,	'no change to number variable');
25
26ok( readonly("fred"),	'string constant');
27
28$var = "fred";
29
30ok( !readonly($var),	'string variable');
31is( $var,	'fred',	'no change to string variable');
32
33$var = \2;
34
35ok( !readonly($var),	'reference to constant');
36ok( readonly($$var),	'de-reference to constant');
37
38ok( !readonly(*STDOUT),	'glob');
39
40sub try
41{
42    my $v = \$_[0];
43    return readonly $$v;
44}
45
46$var = 123;
47{
48    # This used not to work with ithreads, but seems to be working since 5.19.3
49    local $TODO = ( $Config::Config{useithreads} && $] < 5.019003 ) ?
50      "doesn't work with threads" : undef;
51    ok( try ("abc"), 'reference a constant in a sub');
52}
53ok( !try ($var), 'reference a non-constant in a sub');
54