This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regenerate known_pod_issues.dat
[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) = @_;
15     local $::Level = $::Level + 1;
16     my ($rgot, $wgot) = $obj->init();
17     is ($rgot, $rexp);
18     is ($wgot, $wexp);
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 # was a glob
54 my $tied_to = tied $c;
55 $c = *strat;
56 $s = $c;
57 ok($s eq *strat,
58    'Assignment should not ignore magic when the last thing assigned was a glob');
59 expected_tie_calls($tied_to, 1, 1);
60
61 # A plain *foo should not call get-magic on *foo.
62 # This method of scalar-tying an immutable glob relies on details of the
63 # current implementation that are subject to change. This test may need to
64 # be rewritten if they do change.
65 my $tyre = tie $::{gelp} => 'Tie::Monitor';
66 # Compilation of this eval autovivifies the *gelp glob.
67 eval '$tyre->init(0); () = \*gelp';
68 my($rgot, $wgot) = $tyre->init(0);
69 ok($rgot == 0, 'a plain *foo causes no get-magic');
70 ok($wgot == 0, 'a plain *foo causes no set-magic');
71
72 # get-magic when exiting a non-lvalue sub in potentially autovivify-
73 # ing context
74 $tied_to = tie $_{elem}, "Tie::Monitor";
75 eval { () = sub { delete $_{elem} }->()->[3] };
76 ok +($tied_to->init)[0],
77  'get-magic is called on mortal magic var on sub exit in autoviv context';
78 $tied_to = tie $_{elem}, "Tie::Monitor";
79 eval { () = sub { return delete $_{elem} }->()->[3] };
80 ok +($tied_to->init)[0],
81  'get-magic is called on mortal magic var on return in autoviv context';
82
83 done_testing();
84
85 # adapted from Tie::Counter by Abigail
86 package Tie::Monitor;
87
88 sub TIESCALAR {
89     my($class, $value) = @_;
90     bless {
91         read => 0,
92         write => 0,
93         values => [ 0 ],
94     };
95 }
96
97 sub FETCH {
98     my $self = shift;
99     ++$self->{read};
100     $self->{values}[$#{ $self->{values} }];
101 }
102
103 sub STORE {
104     my($self, $value) = @_;
105     ++$self->{write};
106     push @{ $self->{values} }, $value;
107 }
108
109 sub init {
110     my $self = shift;
111     my @results = ($self->{read}, $self->{write});
112     $self->{read} = $self->{write} = 0;
113     $self->{values} = [ 0 ];
114     @results;
115 }