12 package FetchStoreCounter {
13 sub new { my $class = shift; return bless [@_], $class }
14 sub TIESCALAR { return shift->new(@_) }
15 sub FETCH { ${shift->[0]}++ }
16 sub STORE { ${shift->[1]}++ }
21 use builtin qw( true false isbool );
23 ok(true, 'true is true');
24 ok(!false, 'false is false');
26 ok(isbool(true), 'true is bool');
27 ok(isbool(false), 'false is bool');
28 ok(!isbool(undef), 'undef is not bool');
29 ok(!isbool(1), '1 is not bool');
30 ok(!isbool(""), 'empty is not bool');
32 my $truevar = (5 == 5);
33 my $falsevar = (5 == 6);
35 ok(isbool($truevar), '$truevar is bool');
36 ok(isbool($falsevar), '$falsevar is bool');
38 ok(isbool(isbool(true)), 'isbool true is bool');
39 ok(isbool(isbool(123)), 'isbool false is bool');
43 tie my $tied, FetchStoreCounter => (\my $fetchcount, \my $storecount);
45 my $_dummy = isbool($tied);
46 is($fetchcount, 1, 'isbool() invokes FETCH magic');
48 $tied = isbool(false);
49 is($storecount, 1, 'isbool() TARG invokes STORE magic');
54 use builtin qw( isweak weaken unweaken );
59 ok(!isweak($ref), 'ref is not weak initially');
62 ok(isweak($ref), 'ref is weak after weaken()');
65 ok(!isweak($ref), 'ref is not weak after unweaken()');
69 is($ref, undef, 'ref is now undef after arr is cleared');
74 use builtin qw( refaddr reftype blessed );
77 my $obj = bless [], "Object";
79 is(refaddr($arr), $arr+0, 'refaddr yields same as ref in numeric context');
80 is(refaddr("not a ref"), undef, 'refaddr yields undef for non-reference');
82 is(reftype($arr), "ARRAY", 'reftype yields type string');
83 is(reftype($obj), "ARRAY", 'reftype yields basic container type for blessed object');
84 is(reftype("not a ref"), undef, 'reftype yields undef for non-reference');
86 is(blessed($arr), undef, 'blessed yields undef for non-object');
87 is(blessed($obj), "Object", 'blessed yields package name for object');
89 # blessed() as a boolean
90 is(blessed($obj) ? "YES" : "NO", "YES", 'blessed in boolean context still works');
92 # blessed() appears false as a boolean on package "0"
93 is(blessed(bless [], "0") ? "YES" : "NO", "NO", 'blessed in boolean context handles "0" cornercase');
96 # imports are lexical; should not be visible here
98 my $ok = eval 'true()'; my $e = $@;
99 ok(!$ok, 'true() not visible outside of lexical scope');
100 like($e, qr/^Undefined subroutine &main::true called at /, 'failure from true() not visible');
103 # lexical imports work fine in a variety of situations
109 ok(regularfunc(), 'true in regular sub');
115 ok(lexicalfunc(), 'true in lexical sub');
121 ok($coderef->(), 'true in anon sub');
125 return recursefunc() if @_;
128 ok(recursefunc("rec"), 'true in self-recursive sub');
130 my $recursecoderef = sub {
131 use feature 'current_sub';
133 return __SUB__->() if @_;
136 ok($recursecoderef->("rec"), 'true in self-recursive anon sub');
140 use builtin qw( true false );
143 cmp_ok($val, $_, !!1, "true is equivalent to !!1 by $_") for qw( eq == );
144 cmp_ok($val, $_, !0, "true is equivalent to !0 by $_") for qw( eq == );
147 cmp_ok($val, $_, !!0, "false is equivalent to !!0 by $_") for qw( eq == );
148 cmp_ok($val, $_, !1, "false is equivalent to !1 by $_") for qw( eq == );