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 no warnings 'deprecated';
81 sysread $h, $c, 3, 7; # 1 read; 1 write
82 is $c, "*main::bar", 'what sysread wrote'; # 1 read
83 expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
84 close $h or die "$0 cannot close $outfile: $!";
89 # autovivication of aelem, helem, of rv2sv combined with get-magic
93 tie $$s, "Tie::Monitor";
97 expected_tie_calls(tied $$s, 3, 2);
100 tie $a[0], "Tie::Monitor";
104 expected_tie_calls(tied $a[0], 3, 2);
107 tie $h{foo}, "Tie::Monitor";
110 is($h{foo}{bar}, 73);
111 expected_tie_calls(tied $h{foo}, 3, 2);
113 # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
115 ${ (), $$s }[0] = 73;
117 expected_tie_calls(tied $$s, 3, 2);
120 ( ! $true ? undef : $$s )->[0] = 73;
122 expected_tie_calls(tied $$s, 3, 2);
125 ( $true ? $$s : undef )->[0] = 73;
127 expected_tie_calls(tied $$s, 3, 2);
130 # A plain *foo should not call get-magic on *foo.
131 # This method of scalar-tying an immutable glob relies on details of the
132 # current implementation that are subject to change. This test may need to
133 # be rewritten if they do change.
134 my $tyre = tie $::{gelp} => 'Tie::Monitor';
135 # Compilation of this eval autovivifies the *gelp glob.
136 eval '$tyre->init(0); () = \*gelp';
137 my($rgot, $wgot) = $tyre->init(0);
138 ok($rgot == 0, 'a plain *foo causes no get-magic');
139 ok($wgot == 0, 'a plain *foo causes no set-magic');
141 # get-magic when exiting a non-lvalue sub in potentially autovivify-
146 my $tied_to = tie $_{elem}, "Tie::Monitor";
147 () = sub { delete $_{elem} }->()->[3];
148 expected_tie_calls $tied_to, 1, 0,
149 'mortal magic var is implicitly returned in autoviv context';
151 $tied_to = tie $_{elem}, "Tie::Monitor";
152 () = sub { return delete $_{elem} }->()->[3];
153 expected_tie_calls $tied_to, 1, 0,
154 'mortal magic var is explicitly returned in autoviv context';
156 $tied_to = tie $_{elem}, "Tie::Monitor";
158 $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
160 expected_tie_calls $tied_to, 1, 0,
161 'mortal magic var is implicitly returned in recursive autoviv context';
163 $tied_to = tie $_{elem}, "Tie::Monitor";
165 if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
168 expected_tie_calls $tied_to, 1, 0,
169 'mortal magic var is explicitly returned in recursive autoviv context';
171 $tied_to = tie $_{elem}, "Tie::Monitor";
172 my $x = \sub { delete $_{elem} }->();
173 expected_tie_calls $tied_to, 1, 0,
174 'mortal magic var is implicitly returned to refgen';
176 'mortal magic var is copied when implicitly returned';
178 $tied_to = tie $_{elem}, "Tie::Monitor";
179 $x = \sub { return delete $_{elem} }->();
180 expected_tie_calls $tied_to, 1, 0,
181 'mortal magic var is explicitly returned to refgen';
183 'mortal magic var is copied when explicitly returned';
185 $tied_to = tie $_{elem}, "Tie::Monitor";
186 $x = \do { 1; delete $_{elem} };
187 expected_tie_calls $tied_to, 1, 0,
188 'mortal magic var from do passed to refgen';
190 'mortal magic var from do is copied';
195 # adapted from Tie::Counter by Abigail
196 package Tie::Monitor;
199 my($class, $value) = @_;
210 $self->{values}[$#{ $self->{values} }];
214 my($self, $value) = @_;
216 push @{ $self->{values} }, $value;
221 my @results = ($self->{read}, $self->{write});
222 $self->{read} = $self->{write} = 0;
223 $self->{values} = [ 0 ];