This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perl
[perl5.git] / t / op / tie.t
CommitLineData
49d42823 1#!./perl
2
924d8646
CN
3# Add new tests to the end with format:
4# ########
5#
6# # test description
7# Test code
8# EXPECT
9# Warn or die msgs (if any) at - line 1234
10#
49d42823 11
12chdir 't' if -d 't';
20822f61 13@INC = '../lib';
49d42823 14$ENV{PERL5LIB} = "../lib";
15
16$|=1;
17
18undef $/;
924d8646 19@prgs = split /^########\n/m, <DATA>;
49d42823 20
924d8646
CN
21require './test.pl';
22plan(tests => scalar @prgs);
49d42823 23for (@prgs){
924d8646
CN
24 ++$i;
25 my($prog,$expected) = split(/\nEXPECT\n/, $_, 2);
26 print("not ok $i # bad test format\n"), next
27 unless defined $expected;
28 my ($testname) = $prog =~ /^# (.*)\n/m;
29 $testname ||= '';
30 $TODO = $testname =~ s/^TODO //;
49d42823 31 $results =~ s/\n+$//;
32 $expected =~ s/\n+$//;
924d8646
CN
33
34 fresh_perl_is($prog, $expected, {}, $testname);
49d42823 35}
36
37__END__
38
39# standard behaviour, without any extra references
40use Tie::Hash ;
41tie %h, Tie::StdHash;
42untie %h;
43EXPECT
44########
45
a29a5827
NIS
46# standard behaviour, without any extra references
47use Tie::Hash ;
48{package Tie::HashUntie;
49 use base 'Tie::StdHash';
50 sub UNTIE
51 {
52 warn "Untied\n";
53 }
54}
55tie %h, Tie::HashUntie;
56untie %h;
57EXPECT
58Untied
59########
60
49d42823 61# standard behaviour, with 1 extra reference
62use Tie::Hash ;
63$a = tie %h, Tie::StdHash;
64untie %h;
65EXPECT
66########
67
68# standard behaviour, with 1 extra reference via tied
69use Tie::Hash ;
70tie %h, Tie::StdHash;
71$a = tied %h;
72untie %h;
73EXPECT
74########
75
76# standard behaviour, with 1 extra reference which is destroyed
77use Tie::Hash ;
78$a = tie %h, Tie::StdHash;
79$a = 0 ;
80untie %h;
81EXPECT
82########
83
84# standard behaviour, with 1 extra reference via tied which is destroyed
85use Tie::Hash ;
86tie %h, Tie::StdHash;
87$a = tied %h;
88$a = 0 ;
89untie %h;
90EXPECT
91########
92
93# strict behaviour, without any extra references
4438c4b7 94use warnings 'untie';
49d42823 95use Tie::Hash ;
96tie %h, Tie::StdHash;
97untie %h;
98EXPECT
99########
100
101# strict behaviour, with 1 extra references generating an error
4438c4b7 102use warnings 'untie';
49d42823 103use Tie::Hash ;
104$a = tie %h, Tie::StdHash;
105untie %h;
106EXPECT
924d8646 107untie attempted while 1 inner references still exist at - line 6.
49d42823 108########
109
110# strict behaviour, with 1 extra references via tied generating an error
4438c4b7 111use warnings 'untie';
49d42823 112use Tie::Hash ;
113tie %h, Tie::StdHash;
114$a = tied %h;
115untie %h;
116EXPECT
924d8646 117untie attempted while 1 inner references still exist at - line 7.
49d42823 118########
119
120# strict behaviour, with 1 extra references which are destroyed
4438c4b7 121use warnings 'untie';
49d42823 122use Tie::Hash ;
123$a = tie %h, Tie::StdHash;
124$a = 0 ;
125untie %h;
126EXPECT
127########
128
129# strict behaviour, with extra 1 references via tied which are destroyed
4438c4b7 130use warnings 'untie';
49d42823 131use Tie::Hash ;
132tie %h, Tie::StdHash;
133$a = tied %h;
134$a = 0 ;
135untie %h;
136EXPECT
137########
138
498ea806 139# strict error behaviour, with 2 extra references
4438c4b7 140use warnings 'untie';
49d42823 141use Tie::Hash ;
142$a = tie %h, Tie::StdHash;
143$b = tied %h ;
144untie %h;
145EXPECT
924d8646 146untie attempted while 2 inner references still exist at - line 7.
49d42823 147########
148
149# strict behaviour, check scope of strictness.
4438c4b7 150no warnings 'untie';
49d42823 151use Tie::Hash ;
152$A = tie %H, Tie::StdHash;
153$C = $B = tied %H ;
154{
4438c4b7 155 use warnings 'untie';
49d42823 156 use Tie::Hash ;
157 tie %h, Tie::StdHash;
158 untie %h;
159}
160untie %H;
161EXPECT
33c27489 162########
924d8646 163
ae21d580 164# Forbidden aggregate self-ties
33c27489 165sub Self::TIEHASH { bless $_[1], $_[0] }
ae21d580 166{
924d8646 167 my %c;
ae21d580
JH
168 tie %c, 'Self', \%c;
169}
170EXPECT
924d8646 171Self-ties of arrays and hashes are not supported at - line 6.
ae21d580 172########
924d8646 173
ae21d580 174# Allowed scalar self-ties
924d8646 175my $destroyed = 0;
ae21d580 176sub Self::TIESCALAR { bless $_[1], $_[0] }
924d8646 177sub Self::DESTROY { $destroyed = 1; }
33c27489 178{
ae21d580 179 my $c = 42;
ae21d580 180 tie $c, 'Self', \$c;
33c27489 181}
924d8646 182die "self-tied scalar not DESTROYed" unless $destroyed == 1;
bee2c548
CN
183EXPECT
184########
0358cc2c 185
924d8646 186# TODO Allowed glob self-ties
bee2c548 187my $destroyed = 0;
498ea806 188my $printed = 0;
bee2c548
CN
189sub Self2::TIEHANDLE { bless $_[1], $_[0] }
190sub Self2::DESTROY { $destroyed = 1; }
498ea806 191sub Self2::PRINT { $printed = 1; }
bee2c548
CN
192{
193 use Symbol;
194 my $c = gensym;
195 tie *$c, 'Self2', $c;
498ea806 196 print $c 'Hello';
bee2c548 197}
498ea806
CN
198die "self-tied glob not PRINTed" unless $printed == 1;
199die "self-tied glob not DESTROYed" unless $destroyed == 1;
bee2c548
CN
200EXPECT
201########
0358cc2c 202
bee2c548
CN
203# Allowed IO self-ties
204my $destroyed = 0;
205sub Self3::TIEHANDLE { bless $_[1], $_[0] }
206sub Self3::DESTROY { $destroyed = 1; }
207{
208 use Symbol 'geniosym';
209 my $c = geniosym;
210 tie *$c, 'Self3', $c;
211}
498ea806 212die "self-tied IO not DESTROYed" unless $destroyed == 1;
62822184 213EXPECT
0358cc2c 214########
62822184 215
924d8646
CN
216# Interaction of tie and vec
217
218my ($a, $b);
219use Tie::Scalar;
220tie $a,Tie::StdScalar or die;
221vec($b,1,1)=1;
222$a = $b;
223vec($a,1,1)=0;
224vec($b,1,1)=0;
225die unless $a eq $b;
226EXPECT
227########
228
229# correct unlocalisation of tied hashes (patch #16431)
230use Tie::Hash ;
231tie %tied, Tie::StdHash;
232{ local $hash{'foo'} } warn "plain hash bad unlocalize" if exists $hash{'foo'};
233{ local $tied{'foo'} } warn "tied hash bad unlocalize" if exists $tied{'foo'};
234{ local $ENV{'foo'} } warn "%ENV bad unlocalize" if exists $ENV{'foo'};
235EXPECT
236########
237
238# An attempt at lvalueable barewords broke this
239tie FH, 'main';
240EXPECT
241Can't modify constant item in tie at - line 3, near "'main';"
242Execution of - aborted due to compilation errors.
3541f8c8
CN
243########
244
245# localizing tied hash slices
246$ENV{FooA} = 1;
247$ENV{FooB} = 2;
248print exists $ENV{FooA} ? 1 : 0, "\n";
249print exists $ENV{FooB} ? 2 : 0, "\n";
250print exists $ENV{FooC} ? 3 : 0, "\n";
251{
252 local @ENV{qw(FooA FooC)};
253 print exists $ENV{FooA} ? 4 : 0, "\n";
254 print exists $ENV{FooB} ? 5 : 0, "\n";
255 print exists $ENV{FooC} ? 6 : 0, "\n";
256}
257print exists $ENV{FooA} ? 7 : 0, "\n";
258print exists $ENV{FooB} ? 8 : 0, "\n";
259print exists $ENV{FooC} ? 9 : 0, "\n"; # this should not exist
260EXPECT
2611
2622
2630
2644
2655
2666
2677
2688
2690