This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid sysread()/syswrite() warnings from the default :utf8 from PERL_UNICODE
[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  # Do this again, with a utf8 handle
80     $c = *foo;                                         # 1 write
81     open $h, "<:utf8", $outfile;
82     no warnings 'deprecated';
83     sysread $h, $c, 3, 7;                              # 1 read; 1 write
84     is $c, "*main::bar", 'what sysread wrote';         # 1 read
85     expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
86     close $h or die "$0 cannot close $outfile: $!";
87
88     unlink_all $outfile;
89 }
90
91 # autovivication of aelem, helem, of rv2sv combined with get-magic
92 {
93     my $true = 1;
94     my $s;
95     tie $$s, "Tie::Monitor";
96     $$s = undef;
97     $$s->[0] = 73;
98     is($$s->[0], 73);
99     expected_tie_calls(tied $$s, 3, 2);
100
101     my @a;
102     tie $a[0], "Tie::Monitor";
103     $a[0] = undef;
104     $a[0][0] = 73;
105     is($a[0][0], 73);
106     expected_tie_calls(tied $a[0], 3, 2);
107
108     my %h;
109     tie $h{foo}, "Tie::Monitor";
110     $h{foo} = undef;
111     $h{foo}{bar} = 73;
112     is($h{foo}{bar}, 73);
113     expected_tie_calls(tied $h{foo}, 3, 2);
114
115     # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
116     $$s = undef;
117     ${ (), $$s }[0] = 73;
118     is( $$s->[0], 73);
119     expected_tie_calls(tied $$s, 3, 2);
120
121     $$s = undef;
122     ( ! $true ? undef : $$s )->[0] = 73;
123     is( $$s->[0], 73);
124     expected_tie_calls(tied $$s, 3, 2);
125
126     $$s = undef;
127     ( $true ? $$s : undef )->[0] = 73;
128     is( $$s->[0], 73);
129     expected_tie_calls(tied $$s, 3, 2);
130 }
131
132 # A plain *foo should not call get-magic on *foo.
133 # This method of scalar-tying an immutable glob relies on details of the
134 # current implementation that are subject to change. This test may need to
135 # be rewritten if they do change.
136 my $tyre = tie $::{gelp} => 'Tie::Monitor';
137 # Compilation of this eval autovivifies the *gelp glob.
138 eval '$tyre->init(0); () = \*gelp';
139 my($rgot, $wgot) = $tyre->init(0);
140 ok($rgot == 0, 'a plain *foo causes no get-magic');
141 ok($wgot == 0, 'a plain *foo causes no set-magic');
142
143 # get-magic when exiting a non-lvalue sub in potentially autovivify-
144 # ing context
145 {
146   no strict;
147
148   my $tied_to = tie $_{elem}, "Tie::Monitor";
149   () = sub { delete $_{elem} }->()->[3];
150   expected_tie_calls $tied_to, 1, 0,
151      'mortal magic var is implicitly returned in autoviv context';
152
153   $tied_to = tie $_{elem}, "Tie::Monitor";
154   () = sub { return delete $_{elem} }->()->[3];
155   expected_tie_calls $tied_to, 1, 0,
156       'mortal magic var is explicitly returned in autoviv context';
157
158   $tied_to = tie $_{elem}, "Tie::Monitor";
159   my $rsub;
160   $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
161   &$rsub;
162   expected_tie_calls $tied_to, 1, 0,
163     'mortal magic var is implicitly returned in recursive autoviv context';
164
165   $tied_to = tie $_{elem}, "Tie::Monitor";
166   $rsub = sub {
167     if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
168   };
169   &$rsub;
170   expected_tie_calls $tied_to, 1, 0,
171     'mortal magic var is explicitly returned in recursive autoviv context';
172
173   $tied_to = tie $_{elem}, "Tie::Monitor";
174   my $x = \sub { delete $_{elem} }->();
175   expected_tie_calls $tied_to, 1, 0,
176      'mortal magic var is implicitly returned to refgen';
177   is tied $$x, undef,
178      'mortal magic var is copied when implicitly returned';
179
180   $tied_to = tie $_{elem}, "Tie::Monitor";
181   $x = \sub { return delete $_{elem} }->();
182   expected_tie_calls $tied_to, 1, 0,
183      'mortal magic var is explicitly returned to refgen';
184   is tied $$x, undef,
185      'mortal magic var is copied when explicitly returned';
186
187   $tied_to = tie $_{elem}, "Tie::Monitor";
188   $x = \do { 1; delete $_{elem} };
189   expected_tie_calls $tied_to, 1, 0,
190      'mortal magic var from do passed to refgen';
191   is tied $$x, undef,
192      'mortal magic var from do is copied';
193 }
194
195 done_testing();
196
197 # adapted from Tie::Counter by Abigail
198 package Tie::Monitor;
199
200 sub TIESCALAR {
201     my($class, $value) = @_;
202     bless {
203         read => 0,
204         write => 0,
205         values => [ 0 ],
206     };
207 }
208
209 sub FETCH {
210     my $self = shift;
211     ++$self->{read};
212     $self->{values}[$#{ $self->{values} }];
213 }
214
215 sub STORE {
216     my($self, $value) = @_;
217     ++$self->{write};
218     push @{ $self->{values} }, $value;
219 }
220
221 sub init {
222     my $self = shift;
223     my @results = ($self->{read}, $self->{write});
224     $self->{read} = $self->{write} = 0;
225     $self->{values} = [ 0 ];
226     @results;
227 }