| 1 |
#!./perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Scalar::Util qw(readonly); |
| 7 |
use Test::More tests => 11; |
| 8 |
|
| 9 |
ok( readonly(1), 'number constant'); |
| 10 |
|
| 11 |
my $var = 2; |
| 12 |
|
| 13 |
ok( !readonly($var), 'number variable'); |
| 14 |
is( $var, 2, 'no change to number variable'); |
| 15 |
|
| 16 |
ok( readonly("fred"), 'string constant'); |
| 17 |
|
| 18 |
$var = "fred"; |
| 19 |
|
| 20 |
ok( !readonly($var), 'string variable'); |
| 21 |
is( $var, 'fred', 'no change to string variable'); |
| 22 |
|
| 23 |
$var = \2; |
| 24 |
|
| 25 |
ok( !readonly($var), 'reference to constant'); |
| 26 |
ok( readonly($$var), 'de-reference to constant'); |
| 27 |
|
| 28 |
ok( !readonly(*STDOUT), 'glob'); |
| 29 |
|
| 30 |
sub try |
| 31 |
{ |
| 32 |
my $v = \$_[0]; |
| 33 |
return readonly $$v; |
| 34 |
} |
| 35 |
|
| 36 |
$var = 123; |
| 37 |
{ |
| 38 |
# This used not to work with ithreads, but seems to be working since 5.19.3 |
| 39 |
local $TODO = ( $Config::Config{useithreads} && $] < 5.019003 ) ? |
| 40 |
"doesn't work with threads" : undef; |
| 41 |
ok( try ("abc"), 'reference a constant in a sub'); |
| 42 |
} |
| 43 |
ok( !try ($var), 'reference a non-constant in a sub'); |