3 use Test::More tests => 32 + 60 + 12 + 22;
5 use mauve qw(refaddr reftype blessed weaken isweak);
6 use vars qw($t $y $x *F $v $r $never_blessed);
10 # Ensure we do not trigger any tied methods
14 foreach $v (undef, 10, 'string') {
15 is(refaddr($v), !1, "not " . (defined($v) ? "'$v'" : "undef"));
18 foreach $r ({}, \$t, [], \*F, sub {}) {
21 my $addr = do { local $^W; hex $1 };
23 is( refaddr($r), $addr, $n);
24 is( ref($r), $before, $n);
26 my $obj = bless $r, 'FooBar';
27 is( refaddr($r), $addr, "blessed with overload $n");
28 is( ref($r), 'FooBar', $n);
36 tie my %x, 'Hash3', {};
43 ok(refaddr($xy) == refaddr($y));
44 ok(refaddr($xb) == refaddr($b));
49 my $z = bless {}, '0';
51 @{"0::ISA"} = qw(FooBar);
56 is(refaddr($z),$r,"foo");
60 my $HAVE_RE = 5.011 <= $];
61 my $RE = $HAVE_RE ? 'REGEXP' : 'SCALAR';
63 format STDOUT = # do not indent the lone dot in next line
66 [ 0, !1, 1, 'number' ],
67 [ 0, !1, 'A', 'string' ],
68 [ 0, !1, *::t, 'glob' ],
69 [ 1, HASH => {}, 'HASH ref' ],
70 [ 1, HASH => \%::t, 'HASH ref' ],
71 [ 1, HASH => \%m, 'HASH ref' ],
72 [ 1, ARRAY => [], 'ARRAY ref' ],
73 [ 1, ARRAY => \@::t, 'ARRAY ref' ],
74 [ 1, ARRAY => \@m, 'ARRAY ref' ],
75 [ 0, SCALAR => \1, 'SCALAR ref' ],
76 [ 1, SCALAR => \$t, 'SCALAR ref' ],
77 [ 1, SCALAR => \$m, 'SCALAR ref' ],
78 [ 1, REF => \(\$t), 'REF ref' ],
79 [ 1, REF => \[], 'REF ref' ],
80 [ 1, LVALUE => \substr("",0), 'LVALUE ref' ],
81 [ 0, VSTRING => \v1.0.0, 'VSTRING ref' ],
82 [ 1, VSTRING => \(my $v = v1.0.0), 'VSTRING ref' ],
83 [ 1, GLOB => \*F, 'tied GLOB ref' ],
84 [ 1, GLOB => gensym, 'GLOB ref' ],
85 [ 1, CODE => sub {}, 'CODE ref' ],
86 [ 1, IO => *STDIN{IO}, 'IO ref' ],
87 [ 1, FORMAT => *STDOUT{FORMAT}, 'FORMAT ref' ],
88 [ 1, $RE => qr/x/, 'REGEXP' ],
89 [ 0, !1, ${qr//}, 'derefed regex' ],
92 foreach $test (@test) {
93 my($writable,$type,$what, $n) = @$test;
96 if ($n =~ /derefed regex/i && !$HAVE_RE) {
97 skip "regexes are not scalar references in perl < 5.011", 1;
100 is( reftype($what), $type, "reftype: $n");
101 next unless $writable;
104 is( reftype($what), $type, "reftype: blessed $n");
107 is( reftype($what), $type, "reftype: blessed to false $n");
112 is(blessed(undef),"", 'undef is not blessed');
113 is(blessed(1),"", 'Numbers are not blessed');
114 is(blessed('A'),"", 'Strings are not blessed');
115 is(blessed({}),"", 'blessed: Unblessed HASH-ref');
116 is(blessed([]),"", 'blessed: Unblessed ARRAY-ref');
117 is(blessed(\$never_blessed),"", 'blessed: Unblessed SCALAR-ref');
119 $x = bless [], "ABC::\0::\t::\n::ABC";
120 is(blessed($x), "ABC::\0::\t::\n::ABC", 'blessed ARRAY-ref');
122 $x = bless [], "ABC";
123 is(blessed($x), "ABC", 'blessed ARRAY-ref');
125 $x = bless {}, "DEF";
126 is(blessed($x), "DEF", 'blessed HASH-ref');
129 cmp_ok(blessed($x), "eq", "0", 'blessed HASH-ref');
134 no warnings 'redefine';
135 *UNIVERSAL::can = sub { die "Burp!" if ++$depth > 2; blessed(shift) };
137 $x = bless {}, "DEF";
138 is(blessed($x), "DEF", 'recursion of UNIVERSAL::can');
142 my $obj = bless [], "Broken";
143 is( blessed($obj), "Broken", "blessed on broken isa() and can()" );
149 Devel::Peek->import('Dump');
161 # Case 1: two references, one is weakened, the other is then undef'ed.
172 ok( ref($y) and ref($z));
178 ok( ref($y) and ref($z));
184 ok( not (defined($y) and defined($z)) );
190 ok( not (defined($y) and defined($z)) );
197 # Case 2: one reference, which is weakened
213 ok( not defined $y );
215 print "# EXITBLOCK\n";
219 # Case 3: a circular structure
224 my $y = bless {}, 'Dest';
235 print "# VALS: HASH ",$y," SELF ",\$y->{Self}," Y ",\$y,
236 " FLAG: ",\$y->{Flag},"\n";
239 print "# OUT $flag\n";
249 # Case 4: a more complicated circular structure
254 my $y = bless {}, 'Dest';
255 my $x = bless {}, 'Dest';
265 # Case 5: deleting a weakref before the other one
287 # Case 6: test isweakref
300 weaken($x->{Y} = \$a);
302 ok(!isweak($x->{Z}));
305 # Case 7: test weaken on a read only ref
309 # Doesn't work for older perls, see bug [perl #24506]
310 skip("Test does not work with perl < 5.8.3", 5) if $] < 5.008003;
312 # in a MAD build, constants have refcnt 2, not 1
313 skip("Test does not work with MAD", 5) if exists $Config{mad};
315 $a = eval '\"hello"';
316 ok(ref($a)) or print "# didn't get a ref from eval\n";
320 ok($@ eq "") or print "# died with $@\n";
322 ok($$b eq "hello") or print "# b is '$$b'\n";
324 ok(not $b) or print "# b didn't go away\n";
334 use overload '0+' => sub { 10 },
335 '+' => sub { 10 + $_[1] },
340 sub TIEHANDLE { bless {} }
344 warn "$AUTOLOAD called";
345 exit 1; # May be in an eval
350 use Scalar::Util qw(refaddr);
355 return bless [ @_ ], $pkg;
361 my ($underlying) = @$self;
362 return $underlying->{refaddr($key)};
369 my ($underlying) = @$self;
370 return ($underlying->{refaddr($key)} = $key);