readonly.t revision 1.2
1#!./perl 2 3use strict; 4use warnings; 5 6use Scalar::Util qw(readonly); 7use Test::More tests => 11; 8 9ok( readonly(1), 'number constant'); 10 11my $var = 2; 12 13ok( !readonly($var), 'number variable'); 14is( $var, 2, 'no change to number variable'); 15 16ok( readonly("fred"), 'string constant'); 17 18$var = "fred"; 19 20ok( !readonly($var), 'string variable'); 21is( $var, 'fred', 'no change to string variable'); 22 23$var = \2; 24 25ok( !readonly($var), 'reference to constant'); 26ok( readonly($$var), 'de-reference to constant'); 27 28ok( !readonly(*STDOUT), 'glob'); 29 30sub 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} 43ok( !try ($var), 'reference a non-constant in a sub'); 44