This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Allow require_error.t be run from the top level
[perl5.git] / t / op / gmagic.t
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
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     print $h "bar\n";
68     close $h or die "$0 cannot close $outfile: $!";    
69
70     $c = *foo;                                         # 1 write
71     open $h, $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: $!";
76
77  # Do this again, with a utf8 handle
78     $c = *foo;                                         # 1 write
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: $!";
85
86     unlink_all $outfile;
87 }
88
89 # autovivication of aelem, helem, of rv2sv combined with get-magic
90 {
91     my $true = 1;
92     my $s;
93     tie $$s, "Tie::Monitor";
94     $$s = undef;
95     $$s->[0] = 73;
96     is($$s->[0], 73);
97     expected_tie_calls(tied $$s, 3, 2);
98
99     my @a;
100     tie $a[0], "Tie::Monitor";
101     $a[0] = undef;
102     $a[0][0] = 73;
103     is($a[0][0], 73);
104     expected_tie_calls(tied $a[0], 3, 2);
105
106     my %h;
107     tie $h{foo}, "Tie::Monitor";
108     $h{foo} = undef;
109     $h{foo}{bar} = 73;
110     is($h{foo}{bar}, 73);
111     expected_tie_calls(tied $h{foo}, 3, 2);
112
113     # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
114     $$s = undef;
115     ${ (), $$s }[0] = 73;
116     is( $$s->[0], 73);
117     expected_tie_calls(tied $$s, 3, 2);
118
119     $$s = undef;
120     ( ! $true ? undef : $$s )->[0] = 73;
121     is( $$s->[0], 73);
122     expected_tie_calls(tied $$s, 3, 2);
123
124     $$s = undef;
125     ( $true ? $$s : undef )->[0] = 73;
126     is( $$s->[0], 73);
127     expected_tie_calls(tied $$s, 3, 2);
128 }
129
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');
140
141 # get-magic when exiting a non-lvalue sub in potentially autovivify-
142 # ing context
143 {
144   no strict;
145
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';
150
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';
155
156   $tied_to = tie $_{elem}, "Tie::Monitor";
157   my $rsub;
158   $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
159   &$rsub;
160   expected_tie_calls $tied_to, 1, 0,
161     'mortal magic var is implicitly returned in recursive autoviv context';
162
163   $tied_to = tie $_{elem}, "Tie::Monitor";
164   $rsub = sub {
165     if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
166   };
167   &$rsub;
168   expected_tie_calls $tied_to, 1, 0,
169     'mortal magic var is explicitly returned in recursive autoviv context';
170
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';
175   is tied $$x, undef,
176      'mortal magic var is copied when implicitly returned';
177
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';
182   is tied $$x, undef,
183      'mortal magic var is copied when explicitly returned';
184
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';
189   is tied $$x, undef,
190      'mortal magic var from do is copied';
191 }
192
193 done_testing();
194
195 # adapted from Tie::Counter by Abigail
196 package Tie::Monitor;
197
198 sub TIESCALAR {
199     my($class, $value) = @_;
200     bless {
201         read => 0,
202         write => 0,
203         values => [ 0 ],
204     };
205 }
206
207 sub FETCH {
208     my $self = shift;
209     ++$self->{read};
210     $self->{values}[$#{ $self->{values} }];
211 }
212
213 sub STORE {
214     my($self, $value) = @_;
215     ++$self->{write};
216     push @{ $self->{values} }, $value;
217 }
218
219 sub init {
220     my $self = shift;
221     my @results = ($self->{read}, $self->{write});
222     $self->{read} = $self->{write} = 0;
223     $self->{values} = [ 0 ];
224     @results;
225 }