This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(perl #125760) fatalize sysread/syswrite/recv/send on :utf8 handles
[perl5.git] / t / op / gmagic.t
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require './test.pl';
6     set_up_inc('../lib');
7 }
8
9 use strict;
10
11 tie my $c => 'Tie::Monitor';
12
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" : ());
19 }
20
21 # Use ok() instead of is(), cmp_ok() etc, to strictly control number of accesses
22 my($r, $s);
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);
27
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);
32 $s = $c . $c;
33 ok($s eq '00', 'concat');
34 expected_tie_calls(tied $c, 2, 0);
35 $r = 'x';
36 $s = $c = $r . 'y';
37 ok($s eq 'xy', 'concat');
38 expected_tie_calls(tied $c, 1, 1);
39 $s = $c = $c . 'x';
40 ok($s eq '0x', 'concat');
41 expected_tie_calls(tied $c, 2, 1);
42 $s = $c = 'x' . $c;
43 ok($s eq 'x0', 'concat');
44 expected_tie_calls(tied $c, 2, 1);
45 $s = $c = $c . $c;
46 ok($s eq '00', 'concat');
47 expected_tie_calls(tied $c, 3, 1);
48
49 $s = chop($c);
50 ok($s eq '0', 'multiple magic in core functions');
51 expected_tie_calls(tied $c, 1, 1);
52
53 $c = *strat;
54 $s = $c;
55 ok($s eq *strat,
56    'Assignment should not ignore magic when the last thing assigned was a glob');
57 expected_tie_calls(tied $c, 1, 1);
58
59 package o { use overload '""' => sub { "foo\n" } }
60 $c = bless [], o::;
61 chomp $c;
62 expected_tie_calls(tied $c, 1, 2, 'chomping a ref');
63
64 {
65     my $outfile = tempfile();
66     open my $h, ">$outfile" or die  "$0 cannot close $outfile: $!";
67     binmode $h;
68     print $h "bar\n";
69     close $h or die "$0 cannot close $outfile: $!";    
70
71     $c = *foo;                                         # 1 write
72     open $h, $outfile;
73     binmode $h;
74     sysread $h, $c, 3, 7;                              # 1 read; 1 write
75     is $c, "*main::bar", 'what sysread wrote';         # 1 read
76     expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
77     close $h or die "$0 cannot close $outfile: $!";
78
79     unlink_all $outfile;
80 }
81
82 # autovivication of aelem, helem, of rv2sv combined with get-magic
83 {
84     my $true = 1;
85     my $s;
86     tie $$s, "Tie::Monitor";
87     $$s = undef;
88     $$s->[0] = 73;
89     is($$s->[0], 73);
90     expected_tie_calls(tied $$s, 3, 2);
91
92     my @a;
93     tie $a[0], "Tie::Monitor";
94     $a[0] = undef;
95     $a[0][0] = 73;
96     is($a[0][0], 73);
97     expected_tie_calls(tied $a[0], 3, 2);
98
99     my %h;
100     tie $h{foo}, "Tie::Monitor";
101     $h{foo} = undef;
102     $h{foo}{bar} = 73;
103     is($h{foo}{bar}, 73);
104     expected_tie_calls(tied $h{foo}, 3, 2);
105
106     # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
107     $$s = undef;
108     ${ (), $$s }[0] = 73;
109     is( $$s->[0], 73);
110     expected_tie_calls(tied $$s, 3, 2);
111
112     $$s = undef;
113     ( ! $true ? undef : $$s )->[0] = 73;
114     is( $$s->[0], 73);
115     expected_tie_calls(tied $$s, 3, 2);
116
117     $$s = undef;
118     ( $true ? $$s : undef )->[0] = 73;
119     is( $$s->[0], 73);
120     expected_tie_calls(tied $$s, 3, 2);
121 }
122
123 # A plain *foo should not call get-magic on *foo.
124 # This method of scalar-tying an immutable glob relies on details of the
125 # current implementation that are subject to change. This test may need to
126 # be rewritten if they do change.
127 my $tyre = tie $::{gelp} => 'Tie::Monitor';
128 # Compilation of this eval autovivifies the *gelp glob.
129 eval '$tyre->init(0); () = \*gelp';
130 my($rgot, $wgot) = $tyre->init(0);
131 ok($rgot == 0, 'a plain *foo causes no get-magic');
132 ok($wgot == 0, 'a plain *foo causes no set-magic');
133
134 # get-magic when exiting a non-lvalue sub in potentially autovivify-
135 # ing context
136 {
137   no strict;
138
139   my $tied_to = tie $_{elem}, "Tie::Monitor";
140   () = sub { delete $_{elem} }->()->[3];
141   expected_tie_calls $tied_to, 1, 0,
142      'mortal magic var is implicitly returned in autoviv context';
143
144   $tied_to = tie $_{elem}, "Tie::Monitor";
145   () = sub { return delete $_{elem} }->()->[3];
146   expected_tie_calls $tied_to, 1, 0,
147       'mortal magic var is explicitly returned in autoviv context';
148
149   $tied_to = tie $_{elem}, "Tie::Monitor";
150   my $rsub;
151   $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
152   &$rsub;
153   expected_tie_calls $tied_to, 1, 0,
154     'mortal magic var is implicitly returned in recursive autoviv context';
155
156   $tied_to = tie $_{elem}, "Tie::Monitor";
157   $rsub = sub {
158     if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
159   };
160   &$rsub;
161   expected_tie_calls $tied_to, 1, 0,
162     'mortal magic var is explicitly returned in recursive autoviv context';
163
164   $tied_to = tie $_{elem}, "Tie::Monitor";
165   my $x = \sub { delete $_{elem} }->();
166   expected_tie_calls $tied_to, 1, 0,
167      'mortal magic var is implicitly returned to refgen';
168   is tied $$x, undef,
169      'mortal magic var is copied when implicitly returned';
170
171   $tied_to = tie $_{elem}, "Tie::Monitor";
172   $x = \sub { return delete $_{elem} }->();
173   expected_tie_calls $tied_to, 1, 0,
174      'mortal magic var is explicitly returned to refgen';
175   is tied $$x, undef,
176      'mortal magic var is copied when explicitly returned';
177
178   $tied_to = tie $_{elem}, "Tie::Monitor";
179   $x = \do { 1; delete $_{elem} };
180   expected_tie_calls $tied_to, 1, 0,
181      'mortal magic var from do passed to refgen';
182   is tied $$x, undef,
183      'mortal magic var from do is copied';
184 }
185
186 # For better or worse, the order in which concat args are fetched varies
187 # depending on their number. In A .= B.C.D, they are fetched in the order
188 # BCDA, while for A .= B, the order is AB (so for a single concat, the LHS
189 # tied arg is FETCH()ed first). Make sure multiconcat preserves current
190 # behaviour.
191
192 package Increment {
193     sub TIESCALAR {  bless [0, 0] }
194     # returns a new value for each FETCH, until the first STORE
195     sub FETCH { my $x = $_[0][0]; $_[0][0]++ unless $_[0][1]; $x }
196     sub STORE { @{$_[0]} = ($_[1],1) }
197
198     my $t;
199     tie $t, 'Increment';
200     my $r;
201     $r = $t . $t;
202     ::is $r, '01', 'Increment 01';
203     $r = "-$t-$t-$t-";
204     ::is $r, '-2-3-4-', 'Increment 234';
205     $t .= "-$t-$t-$t-";
206     ::is $t, '8-5-6-7-', 'Increment 8567';
207 }
208
209 done_testing();
210
211 # adapted from Tie::Counter by Abigail
212 package Tie::Monitor;
213
214 sub TIESCALAR {
215     my($class, $value) = @_;
216     bless {
217         read => 0,
218         write => 0,
219         values => [ 0 ],
220     };
221 }
222
223 sub FETCH {
224     my $self = shift;
225     ++$self->{read};
226     $self->{values}[$#{ $self->{values} }];
227 }
228
229 sub STORE {
230     my($self, $value) = @_;
231     ++$self->{write};
232     push @{ $self->{values} }, $value;
233 }
234
235 sub init {
236     my $self = shift;
237     my @results = ($self->{read}, $self->{write});
238     $self->{read} = $self->{write} = 0;
239     $self->{values} = [ 0 ];
240     @results;
241 }