This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Suppress ‘once’ warning in gmagic.t
[perl5.git] / t / op / gmagic.t
CommitLineData
8d6d96c1
HS
1#!./perl -w
2
3BEGIN {
8d6d96c1
HS
4 chdir 't' if -d 't';
5 @INC = '../lib';
ead3e279 6 require './test.pl';
8d6d96c1
HS
7}
8
ead3e279 9use strict;
8d6d96c1 10
8d6d96c1
HS
11tie my $c => 'Tie::Monitor';
12
ead3e279 13sub expected_tie_calls {
40914f83 14 my ($obj, $rexp, $wexp, $tn) = @_;
ead3e279
NC
15 local $::Level = $::Level + 1;
16 my ($rgot, $wgot) = $obj->init();
40914f83
FC
17 is ($rgot, $rexp, $tn ? "number of fetches when $tn" : ());
18 is ($wgot, $wexp, $tn ? "number of stores when $tn" : ());
8d6d96c1
HS
19}
20
ead3e279 21# Use ok() instead of is(), cmp_ok() etc, to strictly control number of accesses
8d6d96c1 22my($r, $s);
ead3e279
NC
23ok($r = $c + 0 == 0, 'the thing itself');
24expected_tie_calls(tied $c, 1, 0);
25ok($r = "$c" eq '0', 'the thing itself');
26expected_tie_calls(tied $c, 1, 0);
27
28ok($c . 'x' eq '0x', 'concat');
29expected_tie_calls(tied $c, 1, 0);
30ok('x' . $c eq 'x0', 'concat');
31expected_tie_calls(tied $c, 1, 0);
8d6d96c1 32$s = $c . $c;
ead3e279
NC
33ok($s eq '00', 'concat');
34expected_tie_calls(tied $c, 2, 0);
8d6d96c1
HS
35$r = 'x';
36$s = $c = $r . 'y';
ead3e279
NC
37ok($s eq 'xy', 'concat');
38expected_tie_calls(tied $c, 1, 1);
8d6d96c1 39$s = $c = $c . 'x';
ead3e279
NC
40ok($s eq '0x', 'concat');
41expected_tie_calls(tied $c, 2, 1);
8d6d96c1 42$s = $c = 'x' . $c;
ead3e279
NC
43ok($s eq 'x0', 'concat');
44expected_tie_calls(tied $c, 2, 1);
8d6d96c1 45$s = $c = $c . $c;
ead3e279
NC
46ok($s eq '00', 'concat');
47expected_tie_calls(tied $c, 3, 1);
8d6d96c1 48
1e968d83 49$s = chop($c);
ead3e279
NC
50ok($s eq '0', 'multiple magic in core functions');
51expected_tie_calls(tied $c, 1, 1);
1e968d83 52
5cf4b255
FC
53$c = *strat;
54$s = $c;
ead3e279
NC
55ok($s eq *strat,
56 'Assignment should not ignore magic when the last thing assigned was a glob');
074ededa 57expected_tie_calls(tied $c, 1, 1);
5cf4b255 58
fbac7ddf
FC
59package o { use overload '""' => sub { "foo\n" } }
60$c = bless [], o::;
61chomp $c;
62expected_tie_calls(tied $c, 1, 2, 'chomping a ref');
63
e37d6bdb
FC
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
84573ee4 78 $c = *foo; # 1 write
e37d6bdb
FC
79 open $h, "<:utf8", $outfile;
80 sysread $h, $c, 3, 7; # 1 read; 1 write
81 is $c, "*main::bar", 'what sysread wrote'; # 1 read
82 expected_tie_calls(tied $c, 2, 2, 'calling sysread with tied buf');
83 close $h or die "$0 cannot close $outfile: $!";
84
85 unlink_all $outfile;
86}
87
835c0338
GG
88# autovivication of aelem, helem, of rv2sv combined with get-magic
89{
90 my $true = 1;
91 my $s;
92 tie $$s, "Tie::Monitor";
93 $$s = undef;
94 $$s->[0] = 73;
95 is($$s->[0], 73);
96 expected_tie_calls(tied $$s, 2, 2);
97
98 my @a;
99 tie $a[0], "Tie::Monitor";
100 $a[0] = undef;
101 $a[0][0] = 73;
102 is($a[0][0], 73);
103 expected_tie_calls(tied $a[0], 2, 2);
104
105 my %h;
106 tie $h{foo}, "Tie::Monitor";
107 $h{foo} = undef;
108 $h{foo}{bar} = 73;
109 is($h{foo}{bar}, 73);
110 expected_tie_calls(tied $h{foo}, 2, 2);
111
112 # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
6fee5903
FC
113 $$s = undef;
114 ${ (), $$s }[0] = 73;
115 is( $$s->[0], 73);
116 expected_tie_calls(tied $$s, 2, 2);
835c0338
GG
117
118 $$s = undef;
119 ( ! $true ? undef : $$s )->[0] = 73;
120 is( $$s->[0], 73);
121 expected_tie_calls(tied $$s, 2, 2);
122
6fee5903
FC
123 $$s = undef;
124 ( $true ? $$s : undef )->[0] = 73;
125 is( $$s->[0], 73);
126 expected_tie_calls(tied $$s, 2, 2);
835c0338
GG
127}
128
f64c9ac5
FC
129# A plain *foo should not call get-magic on *foo.
130# This method of scalar-tying an immutable glob relies on details of the
131# current implementation that are subject to change. This test may need to
132# be rewritten if they do change.
133my $tyre = tie $::{gelp} => 'Tie::Monitor';
134# Compilation of this eval autovivifies the *gelp glob.
135eval '$tyre->init(0); () = \*gelp';
136my($rgot, $wgot) = $tyre->init(0);
ead3e279
NC
137ok($rgot == 0, 'a plain *foo causes no get-magic');
138ok($wgot == 0, 'a plain *foo causes no set-magic');
f64c9ac5 139
767eda44
FC
140# get-magic when exiting a non-lvalue sub in potentially autovivify-
141# ing context
40914f83
FC
142{
143 no strict;
144
145 my $tied_to = tie $_{elem}, "Tie::Monitor";
146 () = sub { delete $_{elem} }->()->[3];
147 expected_tie_calls $tied_to, 1, 0,
148 'mortal magic var is implicitly returned in autoviv context';
149
150 $tied_to = tie $_{elem}, "Tie::Monitor";
151 () = sub { return delete $_{elem} }->()->[3];
152 expected_tie_calls $tied_to, 1, 0,
153 'mortal magic var is explicitly returned in autoviv context';
862b2c43
FC
154
155 $tied_to = tie $_{elem}, "Tie::Monitor";
156 my $rsub;
157 $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
158 &$rsub;
159 expected_tie_calls $tied_to, 1, 0,
49b82a38 160 'mortal magic var is implicitly returned in recursive autoviv context';
862b2c43
FC
161
162 $tied_to = tie $_{elem}, "Tie::Monitor";
163 $rsub = sub {
164 if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
165 };
166 &$rsub;
167 expected_tie_calls $tied_to, 1, 0,
49b82a38 168 'mortal magic var is explicitly returned in recursive autoviv context';
40914f83 169}
767eda44 170
ead3e279 171done_testing();
5cf4b255 172
8d6d96c1
HS
173# adapted from Tie::Counter by Abigail
174package Tie::Monitor;
175
176sub TIESCALAR {
177 my($class, $value) = @_;
178 bless {
179 read => 0,
180 write => 0,
181 values => [ 0 ],
182 };
183}
184
185sub FETCH {
186 my $self = shift;
187 ++$self->{read};
188 $self->{values}[$#{ $self->{values} }];
189}
190
191sub STORE {
192 my($self, $value) = @_;
193 ++$self->{write};
194 push @{ $self->{values} }, $value;
195}
196
197sub init {
198 my $self = shift;
199 my @results = ($self->{read}, $self->{write});
200 $self->{read} = $self->{write} = 0;
201 $self->{values} = [ 0 ];
202 @results;
203}