This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Call FETCH once when chomping a tied ref
[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
835c0338
GG
64# autovivication of aelem, helem, of rv2sv combined with get-magic
65{
66 my $true = 1;
67 my $s;
68 tie $$s, "Tie::Monitor";
69 $$s = undef;
70 $$s->[0] = 73;
71 is($$s->[0], 73);
72 expected_tie_calls(tied $$s, 2, 2);
73
74 my @a;
75 tie $a[0], "Tie::Monitor";
76 $a[0] = undef;
77 $a[0][0] = 73;
78 is($a[0][0], 73);
79 expected_tie_calls(tied $a[0], 2, 2);
80
81 my %h;
82 tie $h{foo}, "Tie::Monitor";
83 $h{foo} = undef;
84 $h{foo}{bar} = 73;
85 is($h{foo}{bar}, 73);
86 expected_tie_calls(tied $h{foo}, 2, 2);
87
88 # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
6fee5903
FC
89 $$s = undef;
90 ${ (), $$s }[0] = 73;
91 is( $$s->[0], 73);
92 expected_tie_calls(tied $$s, 2, 2);
835c0338
GG
93
94 $$s = undef;
95 ( ! $true ? undef : $$s )->[0] = 73;
96 is( $$s->[0], 73);
97 expected_tie_calls(tied $$s, 2, 2);
98
6fee5903
FC
99 $$s = undef;
100 ( $true ? $$s : undef )->[0] = 73;
101 is( $$s->[0], 73);
102 expected_tie_calls(tied $$s, 2, 2);
835c0338
GG
103}
104
f64c9ac5
FC
105# A plain *foo should not call get-magic on *foo.
106# This method of scalar-tying an immutable glob relies on details of the
107# current implementation that are subject to change. This test may need to
108# be rewritten if they do change.
109my $tyre = tie $::{gelp} => 'Tie::Monitor';
110# Compilation of this eval autovivifies the *gelp glob.
111eval '$tyre->init(0); () = \*gelp';
112my($rgot, $wgot) = $tyre->init(0);
ead3e279
NC
113ok($rgot == 0, 'a plain *foo causes no get-magic');
114ok($wgot == 0, 'a plain *foo causes no set-magic');
f64c9ac5 115
767eda44
FC
116# get-magic when exiting a non-lvalue sub in potentially autovivify-
117# ing context
40914f83
FC
118{
119 no strict;
120
121 my $tied_to = tie $_{elem}, "Tie::Monitor";
122 () = sub { delete $_{elem} }->()->[3];
123 expected_tie_calls $tied_to, 1, 0,
124 'mortal magic var is implicitly returned in autoviv context';
125
126 $tied_to = tie $_{elem}, "Tie::Monitor";
127 () = sub { return delete $_{elem} }->()->[3];
128 expected_tie_calls $tied_to, 1, 0,
129 'mortal magic var is explicitly returned in autoviv context';
862b2c43
FC
130
131 $tied_to = tie $_{elem}, "Tie::Monitor";
132 my $rsub;
133 $rsub = sub { if ($_[0]) { delete $_{elem} } else { &$rsub(1)->[3] } };
134 &$rsub;
135 expected_tie_calls $tied_to, 1, 0,
49b82a38 136 'mortal magic var is implicitly returned in recursive autoviv context';
862b2c43
FC
137
138 $tied_to = tie $_{elem}, "Tie::Monitor";
139 $rsub = sub {
140 if ($_[0]) { return delete $_{elem} } else { &$rsub(1)->[3] }
141 };
142 &$rsub;
143 expected_tie_calls $tied_to, 1, 0,
49b82a38 144 'mortal magic var is explicitly returned in recursive autoviv context';
40914f83 145}
767eda44 146
ead3e279 147done_testing();
5cf4b255 148
8d6d96c1
HS
149# adapted from Tie::Counter by Abigail
150package Tie::Monitor;
151
152sub TIESCALAR {
153 my($class, $value) = @_;
154 bless {
155 read => 0,
156 write => 0,
157 values => [ 0 ],
158 };
159}
160
161sub FETCH {
162 my $self = shift;
163 ++$self->{read};
164 $self->{values}[$#{ $self->{values} }];
165}
166
167sub STORE {
168 my($self, $value) = @_;
169 ++$self->{write};
170 push @{ $self->{values} }, $value;
171}
172
173sub init {
174 my $self = shift;
175 my @results = ($self->{read}, $self->{write});
176 $self->{read} = $self->{write} = 0;
177 $self->{values} = [ 0 ];
178 @results;
179}