11 tie my $c => 'Tie::Monitor';
13 sub expected_tie_calls {
14 my ($obj, $rexp, $wexp, $tn) = @_;
15 local $::Level = $::Level + 1;
16 my ($rgot, $wgot) = $obj->init();
17 is ($rgot, $rexp, $tn ? "number of fetches when $tn" : ());
18 is ($wgot, $wexp, $tn ? "number of stores when $tn" : ());
21 # Use ok() instead of is(), cmp_ok() etc, to strictly control number of accesses
23 ok($r = $c + 0 == 0, 'the thing itself');
24 expected_tie_calls(tied $c, 1, 0);
25 ok($r = "$c" eq '0', 'the thing itself');
26 expected_tie_calls(tied $c, 1, 0);
28 ok($c . 'x' eq '0x', 'concat');
29 expected_tie_calls(tied $c, 1, 0);
30 ok('x' . $c eq 'x0', 'concat');
31 expected_tie_calls(tied $c, 1, 0);
33 ok($s eq '00', 'concat');
34 expected_tie_calls(tied $c, 2, 0);
37 ok($s eq 'xy', 'concat');
38 expected_tie_calls(tied $c, 1, 1);
40 ok($s eq '0x', 'concat');
41 expected_tie_calls(tied $c, 2, 1);
43 ok($s eq 'x0', 'concat');
44 expected_tie_calls(tied $c, 2, 1);
46 ok($s eq '00', 'concat');
47 expected_tie_calls(tied $c, 3, 1);
50 ok($s eq '0', 'multiple magic in core functions');
51 expected_tie_calls(tied $c, 1, 1);
56 'Assignment should not ignore magic when the last thing assigned was a glob');
57 expected_tie_calls(tied $c, 1, 1);
59 package o { use overload '""' => sub { "foo\n" } }
62 expected_tie_calls(tied $c, 1, 2, 'chomping a ref');
65 my $outfile = tempfile();
66 open my $h, ">$outfile" or die "$0 cannot close $outfile: $!";
68 close $h or die "$0 cannot close $outfile: $!";
72 sysread $h, $c, 3, 7; # 1 read; 1 write
73 is $c, "*main::bar", 'what sysread wrote'; # 1 read
74 expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
75 close $h or die "$0 cannot close $outfile: $!";
77 # Do this again, with a utf8 handle
79 open $h, "<:utf8", $outfile;
80 sysread $h, $c, 3, 7; # 1 read; 1 write
81 is $c, "*main::bar", 'what sysread wrote'; # 1 read
82 expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
83 close $h or die "$0 cannot close $outfile: $!";
88 # autovivication of aelem, helem, of rv2sv combined with get-magic
92 tie $$s, "Tie::Monitor";
96 expected_tie_calls(tied $$s, 3, 2);
99 tie $a[0], "Tie::Monitor";
103 expected_tie_calls(tied $a[0], 3, 2);
106 tie $h{foo}, "Tie::Monitor";
109 is($h{foo}{bar}, 73);
110 expected_tie_calls(tied $h{foo}, 3, 2);
112 # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
114 ${ (), $$s }[0] = 73;
116 expected_tie_calls(tied $$s, 3, 2);
119 ( ! $true ? undef : $$s )->[0] = 73;
121 expected_tie_calls(tied $$s, 3, 2);
124 ( $true ? $$s : undef )->[0] = 73;
126 expected_tie_calls(tied $$s, 3, 2);
129 # A plain *foo should not call get-magic on *foo.
130 # This method of scalar-tying an immutable glob relies on details of the
131 # current implementation that are subject to change. This test may need to
132 # be rewritten if they do change.
133 my $tyre = tie $::{gelp} => 'Tie::Monitor';
134 # Compilation of this eval autovivifies the *gelp glob.
135 eval '$tyre->init(0); () = \*gelp';
136 my($rgot, $wgot) = $tyre->init(0);
137 ok($rgot == 0, 'a plain *foo causes no get-magic');
138 ok($wgot == 0, 'a plain *foo causes no set-magic');
140 # get-magic when exiting a non-lvalue sub in potentially autovivify-
145 my $tied_to = tie $_{elem}, "Tie::Monitor";
146 () = sub { delete $_{elem} }->()->[3];
147 expected_tie_calls $tied_to, 1, 0,
148 'mortal magic var is implicitly returned in autoviv context';
150 $tied_to = tie $_{elem}, "Tie::Monitor";
151 () = sub { return delete $_{elem} }->()->[3];
152 expected_tie_calls $tied_to, 1, 0,
153 'mortal magic var is explicitly returned in autoviv context';
155 $tied_to = tie $_{elem}, "Tie::Monitor";
157 $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
159 expected_tie_calls $tied_to, 1, 0,
160 'mortal magic var is implicitly returned in recursive autoviv context';
162 $tied_to = tie $_{elem}, "Tie::Monitor";
164 if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
167 expected_tie_calls $tied_to, 1, 0,
168 'mortal magic var is explicitly returned in recursive autoviv context';
170 $tied_to = tie $_{elem}, "Tie::Monitor";
171 my $x = \sub { delete $_{elem} }->();
172 expected_tie_calls $tied_to, 1, 0,
173 'mortal magic var is implicitly returned to refgen';
175 'mortal magic var is copied when implicitly returned';
177 $tied_to = tie $_{elem}, "Tie::Monitor";
178 $x = \sub { return delete $_{elem} }->();
179 expected_tie_calls $tied_to, 1, 0,
180 'mortal magic var is explicitly returned to refgen';
182 'mortal magic var is copied when explicitly returned';
187 # adapted from Tie::Counter by Abigail
188 package Tie::Monitor;
191 my($class, $value) = @_;
202 $self->{values}[$#{ $self->{values} }];
206 my($self, $value) = @_;
208 push @{ $self->{values} }, $value;
213 my @results = ($self->{read}, $self->{write});
214 $self->{read} = $self->{write} = 0;
215 $self->{values} = [ 0 ];