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 no warnings 'once'; # main::foo
66 my $outfile = tempfile();
67 open my $h, ">$outfile" or die "$0 cannot close $outfile: $!";
70 close $h or die "$0 cannot close $outfile: $!";
75 sysread $h, $c, 3, 7; # 1 read; 1 write
76 is $c, "*main::bar", 'what sysread wrote'; # 1 read
77 expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
78 close $h or die "$0 cannot close $outfile: $!";
83 # autovivication of aelem, helem, of rv2sv combined with get-magic
87 tie $$s, "Tie::Monitor";
91 expected_tie_calls(tied $$s, 3, 2);
94 tie $a[0], "Tie::Monitor";
98 expected_tie_calls(tied $a[0], 3, 2);
101 tie $h{foo}, "Tie::Monitor";
104 is($h{foo}{bar}, 73);
105 expected_tie_calls(tied $h{foo}, 3, 2);
107 # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
109 ${ (), $$s }[0] = 73;
111 expected_tie_calls(tied $$s, 3, 2);
114 ( ! $true ? undef : $$s )->[0] = 73;
116 expected_tie_calls(tied $$s, 3, 2);
119 ( $true ? $$s : undef )->[0] = 73;
121 expected_tie_calls(tied $$s, 3, 2);
124 # A plain *foo should not call get-magic on *foo.
125 # This method of scalar-tying an immutable glob relies on details of the
126 # current implementation that are subject to change. This test may need to
127 # be rewritten if they do change.
128 my $tyre = tie $::{gelp} => 'Tie::Monitor';
129 # Compilation of this eval autovivifies the *gelp glob.
130 eval '$tyre->init(0); () = \*gelp';
131 my($rgot, $wgot) = $tyre->init(0);
132 ok($rgot == 0, 'a plain *foo causes no get-magic');
133 ok($wgot == 0, 'a plain *foo causes no set-magic');
135 # get-magic when exiting a non-lvalue sub in potentially autovivify-
140 my $tied_to = tie $_{elem}, "Tie::Monitor";
141 () = sub { delete $_{elem} }->()->[3];
142 expected_tie_calls $tied_to, 1, 0,
143 'mortal magic var is implicitly returned in autoviv context';
145 $tied_to = tie $_{elem}, "Tie::Monitor";
146 () = sub { return delete $_{elem} }->()->[3];
147 expected_tie_calls $tied_to, 1, 0,
148 'mortal magic var is explicitly returned in autoviv context';
150 $tied_to = tie $_{elem}, "Tie::Monitor";
152 $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
154 expected_tie_calls $tied_to, 1, 0,
155 'mortal magic var is implicitly returned in recursive autoviv context';
157 $tied_to = tie $_{elem}, "Tie::Monitor";
159 if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
162 expected_tie_calls $tied_to, 1, 0,
163 'mortal magic var is explicitly returned in recursive autoviv context';
165 $tied_to = tie $_{elem}, "Tie::Monitor";
166 my $x = \sub { delete $_{elem} }->();
167 expected_tie_calls $tied_to, 1, 0,
168 'mortal magic var is implicitly returned to refgen';
170 'mortal magic var is copied when implicitly returned';
172 $tied_to = tie $_{elem}, "Tie::Monitor";
173 $x = \sub { return delete $_{elem} }->();
174 expected_tie_calls $tied_to, 1, 0,
175 'mortal magic var is explicitly returned to refgen';
177 'mortal magic var is copied when explicitly returned';
179 $tied_to = tie $_{elem}, "Tie::Monitor";
180 $x = \do { 1; delete $_{elem} };
181 expected_tie_calls $tied_to, 1, 0,
182 'mortal magic var from do passed to refgen';
184 'mortal magic var from do is copied';
187 # For better or worse, the order in which concat args are fetched varies
188 # depending on their number. In A .= B.C.D, they are fetched in the order
189 # BCDA, while for A .= B, the order is AB (so for a single concat, the LHS
190 # tied arg is FETCH()ed first). Make sure multiconcat preserves current
194 sub TIESCALAR { bless [0, 0] }
195 # returns a new value for each FETCH, until the first STORE
196 sub FETCH { my $x = $_[0][0]; $_[0][0]++ unless $_[0][1]; $x }
197 sub STORE { @{$_[0]} = ($_[1],1) }
203 ::is $r, '01', 'Increment 01';
205 ::is $r, '-2-3-4-', 'Increment 234';
207 ::is $t, '8-5-6-7-', 'Increment 8567';
212 # adapted from Tie::Counter by Abigail
213 package Tie::Monitor;
216 my($class, $value) = @_;
227 $self->{values}[$#{ $self->{values} }];
231 my($self, $value) = @_;
233 push @{ $self->{values} }, $value;
238 my @results = ($self->{read}, $self->{write});
239 $self->{read} = $self->{write} = 0;
240 $self->{values} = [ 0 ];