1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#!./perl -w
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
}
use strict;
tie my $c => 'Tie::Monitor';
sub expected_tie_calls {
my ($obj, $rexp, $wexp) = @_;
local $::Level = $::Level + 1;
my ($rgot, $wgot) = $obj->init();
is ($rgot, $rexp);
is ($wgot, $wexp);
}
# Use ok() instead of is(), cmp_ok() etc, to strictly control number of accesses
my($r, $s);
ok($r = $c + 0 == 0, 'the thing itself');
expected_tie_calls(tied $c, 1, 0);
ok($r = "$c" eq '0', 'the thing itself');
expected_tie_calls(tied $c, 1, 0);
ok($c . 'x' eq '0x', 'concat');
expected_tie_calls(tied $c, 1, 0);
ok('x' . $c eq 'x0', 'concat');
expected_tie_calls(tied $c, 1, 0);
$s = $c . $c;
ok($s eq '00', 'concat');
expected_tie_calls(tied $c, 2, 0);
$r = 'x';
$s = $c = $r . 'y';
ok($s eq 'xy', 'concat');
expected_tie_calls(tied $c, 1, 1);
$s = $c = $c . 'x';
ok($s eq '0x', 'concat');
expected_tie_calls(tied $c, 2, 1);
$s = $c = 'x' . $c;
ok($s eq 'x0', 'concat');
expected_tie_calls(tied $c, 2, 1);
$s = $c = $c . $c;
ok($s eq '00', 'concat');
expected_tie_calls(tied $c, 3, 1);
$s = chop($c);
ok($s eq '0', 'multiple magic in core functions');
expected_tie_calls(tied $c, 1, 1);
# was a glob
my $tied_to = tied $c;
$c = *strat;
$s = $c;
ok($s eq *strat,
'Assignment should not ignore magic when the last thing assigned was a glob');
expected_tie_calls($tied_to, 1, 1);
# A plain *foo should not call get-magic on *foo.
# This method of scalar-tying an immutable glob relies on details of the
# current implementation that are subject to change. This test may need to
# be rewritten if they do change.
my $tyre = tie $::{gelp} => 'Tie::Monitor';
# Compilation of this eval autovivifies the *gelp glob.
eval '$tyre->init(0); () = \*gelp';
my($rgot, $wgot) = $tyre->init(0);
ok($rgot == 0, 'a plain *foo causes no get-magic');
ok($wgot == 0, 'a plain *foo causes no set-magic');
# get-magic when exiting a non-lvalue sub in potentially autovivify-
# ing context
$tied_to = tie $_{elem}, "Tie::Monitor";
eval { () = sub { delete $_{elem} }->()->[3] };
ok +($tied_to->init)[0],
'get-magic is called on mortal magic var on sub exit in autoviv context';
$tied_to = tie $_{elem}, "Tie::Monitor";
eval { () = sub { return delete $_{elem} }->()->[3] };
ok +($tied_to->init)[0],
'get-magic is called on mortal magic var on return in autoviv context';
done_testing();
# adapted from Tie::Counter by Abigail
package Tie::Monitor;
sub TIESCALAR {
my($class, $value) = @_;
bless {
read => 0,
write => 0,
values => [ 0 ],
};
}
sub FETCH {
my $self = shift;
++$self->{read};
$self->{values}[$#{ $self->{values} }];
}
sub STORE {
my($self, $value) = @_;
++$self->{write};
push @{ $self->{values} }, $value;
}
sub init {
my $self = shift;
my @results = ($self->{read}, $self->{write});
$self->{read} = $self->{write} = 0;
$self->{values} = [ 0 ];
@results;
}
|