xref: /openbsd-src/gnu/usr.bin/perl/t/op/tie.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!./perl
2
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#
11
12chdir 't' if -d 't';
13@INC = '../lib';
14require './test.pl';
15
16$|=1;
17
18run_multiple_progs('', \*DATA);
19
20done_testing();
21
22__END__
23
24# standard behaviour, without any extra references
25use Tie::Hash ;
26tie %h, Tie::StdHash;
27untie %h;
28EXPECT
29########
30
31# standard behaviour, without any extra references
32use Tie::Hash ;
33{package Tie::HashUntie;
34 use base 'Tie::StdHash';
35 sub UNTIE
36  {
37   warn "Untied\n";
38  }
39}
40tie %h, Tie::HashUntie;
41untie %h;
42EXPECT
43Untied
44########
45
46# standard behaviour, with 1 extra reference
47use Tie::Hash ;
48$a = tie %h, Tie::StdHash;
49untie %h;
50EXPECT
51########
52
53# standard behaviour, with 1 extra reference via tied
54use Tie::Hash ;
55tie %h, Tie::StdHash;
56$a = tied %h;
57untie %h;
58EXPECT
59########
60
61# standard behaviour, with 1 extra reference which is destroyed
62use Tie::Hash ;
63$a = tie %h, Tie::StdHash;
64$a = 0 ;
65untie %h;
66EXPECT
67########
68
69# standard behaviour, with 1 extra reference via tied which is destroyed
70use Tie::Hash ;
71tie %h, Tie::StdHash;
72$a = tied %h;
73$a = 0 ;
74untie %h;
75EXPECT
76########
77
78# strict behaviour, without any extra references
79use warnings 'untie';
80use Tie::Hash ;
81tie %h, Tie::StdHash;
82untie %h;
83EXPECT
84########
85
86# strict behaviour, with 1 extra references generating an error
87use warnings 'untie';
88use Tie::Hash ;
89$a = tie %h, Tie::StdHash;
90untie %h;
91EXPECT
92untie attempted while 1 inner references still exist at - line 6.
93########
94
95# strict behaviour, with 1 extra references via tied generating an error
96use warnings 'untie';
97use Tie::Hash ;
98tie %h, Tie::StdHash;
99$a = tied %h;
100untie %h;
101EXPECT
102untie attempted while 1 inner references still exist at - line 7.
103########
104
105# strict behaviour, with 1 extra references which are destroyed
106use warnings 'untie';
107use Tie::Hash ;
108$a = tie %h, Tie::StdHash;
109$a = 0 ;
110untie %h;
111EXPECT
112########
113
114# strict behaviour, with extra 1 references via tied which are destroyed
115use warnings 'untie';
116use Tie::Hash ;
117tie %h, Tie::StdHash;
118$a = tied %h;
119$a = 0 ;
120untie %h;
121EXPECT
122########
123
124# strict error behaviour, with 2 extra references
125use warnings 'untie';
126use Tie::Hash ;
127$a = tie %h, Tie::StdHash;
128$b = tied %h ;
129untie %h;
130EXPECT
131untie attempted while 2 inner references still exist at - line 7.
132########
133
134# strict behaviour, check scope of strictness.
135no warnings 'untie';
136use Tie::Hash ;
137$A = tie %H, Tie::StdHash;
138$C = $B = tied %H ;
139{
140    use warnings 'untie';
141    use Tie::Hash ;
142    tie %h, Tie::StdHash;
143    untie %h;
144}
145untie %H;
146EXPECT
147########
148
149# Forbidden aggregate self-ties
150sub Self::TIEHASH { bless $_[1], $_[0] }
151{
152    my %c;
153    tie %c, 'Self', \%c;
154}
155EXPECT
156Self-ties of arrays and hashes are not supported at - line 6.
157########
158
159# Allowed scalar self-ties
160my $destroyed = 0;
161sub Self::TIESCALAR { bless $_[1], $_[0] }
162sub Self::DESTROY   { $destroyed = 1; }
163{
164    my $c = 42;
165    tie $c, 'Self', \$c;
166}
167die "self-tied scalar not DESTROYed" unless $destroyed == 1;
168EXPECT
169########
170
171# Allowed glob self-ties
172my $destroyed = 0;
173my $printed   = 0;
174sub Self2::TIEHANDLE { bless $_[1], $_[0] }
175sub Self2::DESTROY   { $destroyed = 1; }
176sub Self2::PRINT     { $printed = 1; }
177{
178    use Symbol;
179    my $c = gensym;
180    tie *$c, 'Self2', $c;
181    print $c 'Hello';
182}
183die "self-tied glob not PRINTed" unless $printed == 1;
184die "self-tied glob not DESTROYed" unless $destroyed == 1;
185EXPECT
186########
187
188# Allowed IO self-ties
189my $destroyed = 0;
190sub Self3::TIEHANDLE { bless $_[1], $_[0] }
191sub Self3::DESTROY   { $destroyed = 1; }
192sub Self3::PRINT     { $printed = 1; }
193{
194    use Symbol 'geniosym';
195    my $c = geniosym;
196    tie *$c, 'Self3', $c;
197    print $c 'Hello';
198}
199die "self-tied IO not PRINTed" unless $printed == 1;
200die "self-tied IO not DESTROYed" unless $destroyed == 1;
201EXPECT
202########
203
204# TODO IO "self-tie" via TEMP glob
205my $destroyed = 0;
206sub Self3::TIEHANDLE { bless $_[1], $_[0] }
207sub Self3::DESTROY   { $destroyed = 1; }
208sub Self3::PRINT     { $printed = 1; }
209{
210    use Symbol 'geniosym';
211    my $c = geniosym;
212    tie *$c, 'Self3', \*$c;
213    print $c 'Hello';
214}
215die "IO tied to TEMP glob not PRINTed" unless $printed == 1;
216die "IO tied to TEMP glob not DESTROYed" unless $destroyed == 1;
217EXPECT
218########
219
220# Interaction of tie and vec
221
222my ($a, $b);
223use Tie::Scalar;
224tie $a,Tie::StdScalar or die;
225vec($b,1,1)=1;
226$a = $b;
227vec($a,1,1)=0;
228vec($b,1,1)=0;
229die unless $a eq $b;
230EXPECT
231########
232
233# correct unlocalisation of tied hashes (patch #16431)
234use Tie::Hash ;
235tie %tied, Tie::StdHash;
236{ local $hash{'foo'} } warn "plain hash bad unlocalize" if exists $hash{'foo'};
237{ local $tied{'foo'} } warn "tied hash bad unlocalize" if exists $tied{'foo'};
238{ local $ENV{'foo'}  } warn "%ENV bad unlocalize" if exists $ENV{'foo'};
239EXPECT
240########
241
242# An attempt at lvalueable barewords broke this
243tie FH, 'main';
244EXPECT
245Can't modify constant item in tie at - line 3, near "'main';"
246Execution of - aborted due to compilation errors.
247########
248
249# localizing tied hash slices
250$ENV{FooA} = 1;
251$ENV{FooB} = 2;
252print exists $ENV{FooA} ? 1 : 0, "\n";
253print exists $ENV{FooB} ? 2 : 0, "\n";
254print exists $ENV{FooC} ? 3 : 0, "\n";
255{
256    local @ENV{qw(FooA FooC)};
257    print exists $ENV{FooA} ? 4 : 0, "\n";
258    print exists $ENV{FooB} ? 5 : 0, "\n";
259    print exists $ENV{FooC} ? 6 : 0, "\n";
260}
261print exists $ENV{FooA} ? 7 : 0, "\n";
262print exists $ENV{FooB} ? 8 : 0, "\n";
263print exists $ENV{FooC} ? 9 : 0, "\n"; # this should not exist
264EXPECT
2651
2662
2670
2684
2695
2706
2717
2728
2730
274########
275#
276# FETCH freeing tie'd SV still works
277sub TIESCALAR { bless [] }
278sub FETCH { *a = \1; 2 }
279tie $a, 'main';
280print $a;
281EXPECT
2822
283########
284
285#  [20020716.007] - nested FETCHES
286
287sub F1::TIEARRAY { bless [], 'F1' }
288sub F1::FETCH { 1 }
289my @f1;
290tie @f1, 'F1';
291
292sub F2::TIEARRAY { bless [2], 'F2' }
293sub F2::FETCH { my $self = shift; my $x = $f1[3]; $self }
294my @f2;
295tie @f2, 'F2';
296
297print $f2[4][0],"\n";
298
299sub F3::TIEHASH { bless [], 'F3' }
300sub F3::FETCH { 1 }
301my %f3;
302tie %f3, 'F3';
303
304sub F4::TIEHASH { bless [3], 'F4' }
305sub F4::FETCH { my $self = shift; my $x = $f3{3}; $self }
306my %f4;
307tie %f4, 'F4';
308
309print $f4{'foo'}[0],"\n";
310
311EXPECT
3122
3133
314########
315# test untie() from within FETCH
316package Foo;
317sub TIESCALAR { my $pkg = shift; return bless [@_], $pkg; }
318sub FETCH {
319  my $self = shift;
320  my ($obj, $field) = @$self;
321  untie $obj->{$field};
322  $obj->{$field} = "Bar";
323}
324package main;
325tie $a->{foo}, "Foo", $a, "foo";
326my $s = $a->{foo}; # access once
327# the hash element should not be tied anymore
328print defined tied $a->{foo} ? "not ok" : "ok";
329EXPECT
330ok
331########
332# the tmps returned by FETCH should appear to be SCALAR
333# (even though they are now implemented using PVLVs.)
334package X;
335sub TIEHASH { bless {} }
336sub TIEARRAY { bless {} }
337sub FETCH {1}
338my (%h, @a);
339tie %h, 'X';
340tie @a, 'X';
341my $r1 = \$h{1};
342my $r2 = \$a[0];
343my $s = "$r1 ". ref($r1) . " $r2 " . ref($r2);
344$s=~ s/\(0x\w+\)//g;
345print $s, "\n";
346EXPECT
347SCALAR SCALAR SCALAR SCALAR
348########
349# [perl #23287] segfault in untie
350sub TIESCALAR { bless $_[1], $_[0] }
351my $var;
352tie $var, 'main', \$var;
353untie $var;
354EXPECT
355########
356# Test case from perlmonks by runrig
357# http://www.perlmonks.org/index.pl?node_id=273490
358# "Here is what I tried. I think its similar to what you've tried
359#  above. Its odd but convenient that after untie'ing you are left with
360#  a variable that has the same value as was last returned from
361#  FETCH. (At least on my perl v5.6.1). So you don't need to pass a
362#  reference to the variable in order to set it after the untie (here it
363#  is accessed through a closure)."
364use strict;
365use warnings;
366package MyTied;
367sub TIESCALAR {
368    my ($class,$code) = @_;
369    bless $code, $class;
370}
371sub FETCH {
372    my $self = shift;
373    print "Untie\n";
374    $self->();
375}
376package main;
377my $var;
378tie $var, 'MyTied', sub { untie $var; 4 };
379print "One\n";
380print "$var\n";
381print "Two\n";
382print "$var\n";
383print "Three\n";
384print "$var\n";
385EXPECT
386One
387Untie
3884
389Two
3904
391Three
3924
393########
394# [perl #22297] cannot untie scalar from within tied FETCH
395my $counter = 0;
396my $x = 7;
397my $ref = \$x;
398tie $x, 'Overlay', $ref, $x;
399my $y;
400$y = $x;
401$y = $x;
402$y = $x;
403$y = $x;
404#print "WILL EXTERNAL UNTIE $ref\n";
405untie $$ref;
406$y = $x;
407$y = $x;
408$y = $x;
409$y = $x;
410#print "counter = $counter\n";
411
412print (($counter == 1) ? "ok\n" : "not ok\n");
413
414package Overlay;
415
416sub TIESCALAR
417{
418        my $pkg = shift;
419        my ($ref, $val) = @_;
420        return bless [ $ref, $val ], $pkg;
421}
422
423sub FETCH
424{
425        my $self = shift;
426        my ($ref, $val) = @$self;
427        #print "WILL INTERNAL UNITE $ref\n";
428        $counter++;
429        untie $$ref;
430        return $val;
431}
432EXPECT
433ok
434########
435
436# [perl #948] cannot meaningfully tie $,
437package TieDollarComma;
438
439sub TIESCALAR {
440     my $pkg = shift;
441     return bless \my $x, $pkg;
442}
443
444sub STORE {
445    my $self = shift;
446    $$self = shift;
447    print "STORE set '$$self'\n";
448}
449
450sub FETCH {
451    my $self = shift;
452    print "<FETCH>";
453    return $$self;
454}
455package main;
456
457tie $,, 'TieDollarComma';
458$, = 'BOBBINS';
459print "join", "things", "up\n";
460EXPECT
461STORE set 'BOBBINS'
462join<FETCH>BOBBINSthings<FETCH>BOBBINSup
463########
464
465# test SCALAR method
466package TieScalar;
467
468sub TIEHASH {
469    my $pkg = shift;
470    bless { } => $pkg;
471}
472
473sub STORE {
474    $_[0]->{$_[1]} = $_[2];
475}
476
477sub FETCH {
478    $_[0]->{$_[1]}
479}
480
481sub CLEAR {
482    %{ $_[0] } = ();
483}
484
485sub SCALAR {
486    print "SCALAR\n";
487    return 0 if ! keys %{$_[0]};
488    sprintf "%i/%i", scalar keys %{$_[0]}, scalar keys %{$_[0]};
489}
490
491package main;
492tie my %h => "TieScalar";
493$h{key1} = "val1";
494$h{key2} = "val2";
495print scalar %h, "\n"
496    if %h; # this should also call SCALAR but implicitly
497%h = ();
498print scalar %h, "\n"
499    if !%h; # this should also call SCALAR but implicitly
500EXPECT
501SCALAR
502SCALAR
5032/2
504SCALAR
505SCALAR
5060
507########
508
509# test scalar on tied hash when no SCALAR method has been given
510package TieScalar;
511
512sub TIEHASH {
513    my $pkg = shift;
514    bless { } => $pkg;
515}
516sub STORE {
517    $_[0]->{$_[1]} = $_[2];
518}
519sub FETCH {
520    $_[0]->{$_[1]}
521}
522sub CLEAR {
523    %{ $_[0] } = ();
524}
525sub FIRSTKEY {
526    my $a = keys %{ $_[0] };
527    print "FIRSTKEY\n";
528    each %{ $_[0] };
529}
530
531package main;
532tie my %h => "TieScalar";
533
534if (!%h) {
535    print "empty\n";
536} else {
537    print "not empty\n";
538}
539
540$h{key1} = "val1";
541print "not empty\n" if %h;
542print "not empty\n" if %h;
543print "-->\n";
544my ($k,$v) = each %h;
545print "<--\n";
546print "not empty\n" if %h;
547%h = ();
548print "empty\n" if ! %h;
549EXPECT
550FIRSTKEY
551empty
552FIRSTKEY
553not empty
554FIRSTKEY
555not empty
556-->
557FIRSTKEY
558<--
559not empty
560FIRSTKEY
561empty
562########
563sub TIESCALAR { bless {} }
564sub FETCH { my $x = 3.3; 1 if 0+$x; $x }
565tie $h, "main";
566print $h,"\n";
567EXPECT
5683.3
569########
570sub TIESCALAR { bless {} }
571sub FETCH { shift()->{i} ++ }
572tie $h, "main";
573print $h.$h;
574EXPECT
57501
576########
577# Bug 53482 (and maybe others)
578sub TIESCALAR { my $foo = $_[1]; bless \$foo, $_[0] }
579sub FETCH { ${$_[0]} }
580tie my $x1, "main", 2;
581tie my $y1, "main", 8;
582print $x1 | $y1;
583print $x1 | $y1;
584tie my $x2, "main", "2";
585tie my $y2, "main", "8";
586print $x2 | $y2;
587print $x2 | $y2;
588EXPECT
5891010::
590########
591# Bug 36267
592sub TIEHASH  { bless {}, $_[0] }
593sub STORE    { $_[0]->{$_[1]} = $_[2] }
594sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
595sub NEXTKEY  { each %{$_[0]} }
596sub DELETE   { delete $_[0]->{$_[1]} }
597sub CLEAR    { %{$_[0]} = () }
598$h{b}=1;
599delete $h{b};
600print scalar keys %h, "\n";
601tie %h, 'main';
602$i{a}=1;
603%h = %i;
604untie %h;
605print scalar keys %h, "\n";
606EXPECT
6070
6080
609########
610# Bug 37731
611sub foo::TIESCALAR { bless {value => $_[1]}, $_[0] }
612sub foo::FETCH { $_[0]->{value} }
613tie my $VAR, 'foo', '42';
614foreach my $var ($VAR) {
615    print +($var eq $VAR) ? "yes\n" : "no\n";
616}
617EXPECT
618yes
619########
620sub TIEARRAY { bless [], 'main' }
621{
622    local @a;
623    tie @a, 'main';
624}
625print "tied\n" if tied @a;
626EXPECT
627########
628sub TIEHASH { bless [], 'main' }
629{
630    local %h;
631    tie %h, 'main';
632}
633print "tied\n" if tied %h;
634EXPECT
635########
636# RT 20727: PL_defoutgv is left as a tied element
637sub TIESCALAR { return bless {}, 'main' }
638
639sub STORE {
640    select($_[1]);
641    $_[1] = 1;
642    select(); # this used to coredump or assert fail
643}
644tie $SELECT, 'main';
645$SELECT = *STDERR;
646EXPECT
647########
648# RT 23810: eval in die in FETCH can corrupt context stack
649
650my $file = 'rt23810.pm';
651
652my $e;
653my $s;
654
655sub do_require {
656    my ($str, $eval) = @_;
657    open my $fh, '>', $file or die "Can't create $file: $!\n";
658    print $fh $str;
659    close $fh;
660    if ($eval) {
661	$s .= '-ERQ';
662	eval { require $pm; $s .= '-ENDE' }
663    }
664    else {
665	$s .= '-RQ';
666	require $pm;
667    }
668    $s .= '-ENDRQ';
669    unlink $file;
670}
671
672sub TIEHASH { bless {} }
673
674sub FETCH {
675    # 10 or more syntax errors makes yyparse croak()
676    my $bad = q{$x+;$x+;$x+;$x+;$x+;$x+;$x+;$x+;$x+$x+;$x+;$x+;$x+;$x+;;$x+;};
677
678    if ($_[1] eq 'eval') {
679	$s .= 'EVAL';
680	eval q[BEGIN { die; $s .= '-X1' }];
681	$s .= '-BD';
682	eval q[BEGIN { $x+ }];
683	$s .= '-BS';
684	eval '$x+';
685	$s .= '-E1';
686	$s .= '-S1' while $@ =~ /syntax error at/g;
687	eval $bad;
688	$s .= '-E2';
689	$s .= '-S2' while $@ =~ /syntax error at/g;
690    }
691    elsif ($_[1] eq 'require') {
692	$s .= 'REQUIRE';
693	my @text = (
694	    q[BEGIN { die; $s .= '-X1' }],
695	    q[BEGIN { $x+ }],
696	    '$x+',
697	    $bad
698	);
699	for my $i (0..$#text) {
700	    $s .= "-$i";
701	    do_require($txt[$i], 0) if $e;;
702	    do_require($txt[$i], 1);
703	}
704    }
705    elsif ($_[1] eq 'exit') {
706	eval q[exit(0); print "overshot eval\n"];
707    }
708    else {
709	print "unknown key: '$_[1]'\n";
710    }
711    return "-R";
712}
713my %foo;
714tie %foo, "main";
715
716for my $action(qw(eval require)) {
717    $s = ''; $e = 0; $s .= main->FETCH($action); print "$action: s0=$s\n";
718    $s = ''; $e = 1; eval { $s .= main->FETCH($action)}; print "$action: s1=$s\n";
719    $s = ''; $e = 0; $s .= $foo{$action}; print "$action: s2=$s\n";
720    $s = ''; $e = 1; eval { $s .= $foo{$action}}; print "$action: s3=$s\n";
721}
7221 while unlink $file;
723
724$foo{'exit'};
725print "overshot main\n"; # shouldn't reach here
726
727EXPECT
728eval: s0=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
729eval: s1=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
730eval: s2=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
731eval: s3=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
732require: s0=REQUIRE-0-ERQ-ENDRQ-1-ERQ-ENDRQ-2-ERQ-ENDRQ-3-ERQ-ENDRQ-R
733require: s1=REQUIRE-0-RQ
734require: s2=REQUIRE-0-ERQ-ENDRQ-1-ERQ-ENDRQ-2-ERQ-ENDRQ-3-ERQ-ENDRQ-R
735require: s3=REQUIRE-0-RQ
736########
737# RT 8857: STORE incorrectly invoked for local($_) on aliased tied array
738#          element
739
740sub TIEARRAY { bless [], $_[0] }
741sub TIEHASH  { bless [], $_[0] }
742sub FETCH { $_[0]->[$_[1]] }
743sub STORE { $_[0]->[$_[1]] = $_[2] }
744
745
746sub f {
747    local $_[0];
748}
749tie @a, 'main';
750tie %h, 'main';
751
752foreach ($a[0], $h{a}) {
753    f($_);
754}
755# on failure, chucks up 'premature free' etc messages
756EXPECT
757########
758# RT 5475:
759# the initial fix for this bug caused tied scalar FETCH to be called
760# multiple times when that scalar was an element in an array. Check it
761# only gets called once now.
762
763sub TIESCALAR { bless [], $_[0] }
764my $c = 0;
765sub FETCH { $c++; 0 }
766sub FETCHSIZE { 1 }
767sub STORE { $c += 100; 0 }
768
769
770my (@a, %h);
771tie $a[0],   'main';
772tie $h{foo}, 'main';
773
774my $i = 0;
775my $x = $a[0] + $h{foo} + $a[$i] + (@a)[0];
776print "x=$x c=$c\n";
777EXPECT
778x=0 c=4
779########
780# Bug 68192 - numeric ops not calling mg_get when tied scalar holds a ref
781sub TIESCALAR { bless {}, __PACKAGE__ };
782sub STORE {};
783sub FETCH {
784 print "fetching... "; # make sure FETCH is called once per op
785 123456
786};
787my $foo;
788tie $foo, __PACKAGE__;
789my $a = [1234567];
790$foo = $a;
791print "+   ", 0 + $foo, "\n";
792print "**  ", $foo**1, "\n";
793print "*   ", $foo*1, "\n";
794print "/   ", $foo*1, "\n";
795print "%   ", $foo%123457, "\n";
796print "-   ", $foo-0, "\n";
797print "neg ", - -$foo, "\n";
798print "int ", int $foo, "\n";
799print "abs ", abs $foo, "\n";
800print "==  ", 123456 == $foo, "\n";
801print "<   ", 123455 < $foo, "\n";
802print ">   ", 123457 > $foo, "\n";
803print "<=  ", 123456 <= $foo, "\n";
804print ">=  ", 123456 >= $foo, "\n";
805print "!=  ", 0 != $foo, "\n";
806print "<=> ", 123457 <=> $foo, "\n";
807EXPECT
808fetching... +   123456
809fetching... **  123456
810fetching... *   123456
811fetching... /   123456
812fetching... %   123456
813fetching... -   123456
814fetching... neg 123456
815fetching... int 123456
816fetching... abs 123456
817fetching... ==  1
818fetching... <   1
819fetching... >   1
820fetching... <=  1
821fetching... >=  1
822fetching... !=  1
823fetching... <=> 1
824########
825# Ties returning overloaded objects
826{
827 package overloaded;
828 use overload
829  '*{}' => sub { print '*{}'; \*100 },
830  '@{}' => sub { print '@{}'; \@100 },
831  '%{}' => sub { print '%{}'; \%100 },
832  '${}' => sub { print '${}'; \$100 },
833  map {
834   my $op = $_;
835   $_ => sub { print "$op"; 100 }
836  } qw< 0+ "" + ** * / % - neg int abs == < > <= >= != <=> <> >
837}
838$o = bless [], overloaded;
839
840sub TIESCALAR { bless {}, "" }
841sub FETCH { print "fetching... "; $o }
842sub STORE{}
843tie $ghew, "";
844
845$ghew=undef; 1+$ghew; print "\n";
846$ghew=undef; $ghew**1; print "\n";
847$ghew=undef; $ghew*1; print "\n";
848$ghew=undef; $ghew/1; print "\n";
849$ghew=undef; $ghew%1; print "\n";
850$ghew=undef; $ghew-1; print "\n";
851$ghew=undef; -$ghew; print "\n";
852$ghew=undef; int $ghew; print "\n";
853$ghew=undef; abs $ghew; print "\n";
854$ghew=undef; 1 == $ghew; print "\n";
855$ghew=undef; $ghew<1; print "\n";
856$ghew=undef; $ghew>1; print "\n";
857$ghew=undef; $ghew<=1; print "\n";
858$ghew=undef; $ghew >=1; print "\n";
859$ghew=undef; $ghew != 1; print "\n";
860$ghew=undef; $ghew<=>1; print "\n";
861$ghew=undef; <$ghew>; print "\n";
862$ghew=\*shrext; *$ghew; print "\n";
863$ghew=\@spled; @$ghew; print "\n";
864$ghew=\%frit; %$ghew; print "\n";
865$ghew=\$drile; $$ghew; print "\n";
866EXPECT
867fetching... +
868fetching... **
869fetching... *
870fetching... /
871fetching... %
872fetching... -
873fetching... neg
874fetching... int
875fetching... abs
876fetching... ==
877fetching... <
878fetching... >
879fetching... <=
880fetching... >=
881fetching... !=
882fetching... <=>
883fetching... <>
884fetching... *{}
885fetching... @{}
886fetching... %{}
887fetching... ${}
888########
889# RT 51636: segmentation fault with array ties
890
891tie my @a, 'T';
892@a = (1);
893print "ok\n"; # if we got here we didn't crash
894
895package T;
896
897sub TIEARRAY { bless {} }
898sub STORE    { tie my @b, 'T' }
899sub CLEAR    { }
900sub EXTEND   { }
901
902EXPECT
903ok
904########
905# RT 8438: Tied scalars don't call FETCH when subref is dereferenced
906
907sub TIESCALAR { bless {} }
908
909my $fetch = 0;
910my $called = 0;
911sub FETCH { $fetch++; sub { $called++ } }
912
913tie my $f, 'main';
914$f->(1) for 1,2;
915print "fetch=$fetch\ncalled=$called\n";
916
917EXPECT
918fetch=2
919called=2
920########
921# tie mustn't attempt to call methods on bareword filehandles.
922sub IO::File::TIEARRAY {
923    die "Did not want to invoke IO::File::TIEARRAY";
924}
925fileno FOO; tie @a, "FOO"
926EXPECT
927Can't locate object method "TIEARRAY" via package "FOO" at - line 5.
928########
929#
930# STORE freeing tie'd AV
931sub TIEARRAY  { bless [] }
932sub STORE     { *a = []; 1 }
933sub STORESIZE { }
934sub EXTEND    { }
935tie @a, 'main';
936$a[0] = 1;
937EXPECT
938########
939#
940# CLEAR freeing tie'd AV
941sub TIEARRAY  { bless [] }
942sub CLEAR     { *a = []; 1 }
943sub STORESIZE { }
944sub EXTEND    { }
945sub STORE     { }
946tie @a, 'main';
947@a = (1,2,3);
948EXPECT
949########
950#
951# FETCHSIZE freeing tie'd AV
952sub TIEARRAY  { bless [] }
953sub FETCHSIZE { *a = []; 100 }
954sub STORESIZE { }
955sub EXTEND    { }
956sub STORE     { }
957tie @a, 'main';
958print $#a,"\n"
959EXPECT
96099
961########
962#
963# [perl #86328] Crash when freeing tie magic that can increment the refcnt
964
965eval { require Scalar::Util } or print("ok\n"), exit;
966
967sub TIEHASH {
968    return $_[1];
969}
970*TIEARRAY = *TIEHASH;
971
972sub DESTROY {
973    my ($tied) = @_;
974    my $b = $tied->[0];
975}
976
977my $a = {};
978my $o = bless [];
979Scalar::Util::weaken($o->[0] = $a);
980tie %$a, "main", $o;
981
982my $b = [];
983my $p = bless [];
984Scalar::Util::weaken($p->[0] = $b);
985tie @$b, "main", $p;
986
987# Done setting up the evil data structures
988
989$a = undef;
990$b = undef;
991print "ok\n";
992
993EXPECT
994ok
995########
996#
997# Localising a tied COW scalar should not make it read-only.
998
999sub TIESCALAR { bless [] }
1000sub FETCH { __PACKAGE__ }
1001sub STORE {}
1002tie $x, "";
1003"$x";
1004{
1005    local $x;
1006    $x = 3;
1007}
1008print "ok\n";
1009EXPECT
1010ok
1011########
1012#
1013# Nor should it be impossible to tie COW scalars that are already PVMGs.
1014
1015sub TIESCALAR { bless [] }
1016$x = *foo;        # PVGV
1017undef $x;         # downgrade to PVMG
1018$x = __PACKAGE__; # PVMG + COW
1019tie $x, "";       # bang!
1020
1021print STDERR "ok\n";
1022
1023# However, one should not be able to tie read-only glob copies, which look
1024# a bit like kine internally (FAKE + READONLY).
1025$y = *foo;
1026Internals::SvREADONLY($y,1);
1027tie $y, "";
1028
1029EXPECT
1030ok
1031Modification of a read-only value attempted at - line 16.
1032########
1033#
1034# And one should not be able to tie read-only COWs
1035for(__PACKAGE__) { tie $_, "" }
1036sub TIESCALAR {bless []}
1037EXPECT
1038Modification of a read-only value attempted at - line 3.
1039########
1040
1041# Similarly, read-only regexps cannot be tied.
1042sub TIESCALAR { bless [] }
1043$y = ${qr//};
1044Internals::SvREADONLY($y,1);
1045tie $y, "";
1046
1047EXPECT
1048Modification of a read-only value attempted at - line 6.
1049########
1050
1051# tied() should still work on tied scalars after glob assignment
1052sub TIESCALAR {bless[]}
1053sub FETCH {*foo}
1054sub f::TIEHANDLE{bless[],f}
1055tie *foo, "f";
1056tie $rin, "";
1057[$rin]; # call FETCH
1058print ref tied $rin, "\n";
1059print ref tied *$rin, "\n";
1060EXPECT
1061main
1062f
1063########
1064
1065# (un)tie $glob_copy vs (un)tie *$glob_copy
1066sub TIESCALAR { print "TIESCALAR\n"; bless [] }
1067sub TIEHANDLE{ print "TIEHANDLE\n"; bless [] }
1068sub FETCH { print "never called\n" }
1069$f = *foo;
1070tie *$f, "";
1071tie $f, "";
1072untie $f;
1073print "ok 1\n" if !tied $f;
1074() = $f; # should not call FETCH
1075untie *$f;
1076print "ok 2\n" if !tied *foo;
1077EXPECT
1078TIEHANDLE
1079TIESCALAR
1080ok 1
1081ok 2
1082########
1083
1084# RT #8611 mustn't goto outside the magic stack
1085sub TIESCALAR { warn "tiescalar\n"; bless [] }
1086sub FETCH { warn "fetch()\n"; goto FOO; }
1087tie $f, "";
1088warn "before fetch\n";
1089my $a = "$f";
1090warn "before FOO\n";
1091FOO:
1092warn "after FOO\n";
1093EXPECT
1094tiescalar
1095before fetch
1096fetch()
1097Can't find label FOO at - line 4.
1098########
1099
1100# RT #8611 mustn't goto outside the magic stack
1101sub TIEHANDLE { warn "tiehandle\n"; bless [] }
1102sub PRINT { warn "print()\n"; goto FOO; }
1103tie *F, "";
1104warn "before print\n";
1105print F "abc";
1106warn "before FOO\n";
1107FOO:
1108warn "after FOO\n";
1109EXPECT
1110tiehandle
1111before print
1112print()
1113Can't find label FOO at - line 4.
1114########
1115
1116# \&$tied with $tied holding a reference before the fetch (but not after)
1117sub ::72 { 73 };
1118sub TIESCALAR {bless[]}
1119sub STORE{}
1120sub FETCH { 72 }
1121tie my $x, "main";
1122$x = \$y;
1123\&$x;
1124print "ok\n";
1125EXPECT
1126ok
1127########
1128
1129# \&$tied with $tied holding a PVLV glob before the fetch (but not after)
1130sub ::72 { 73 };
1131sub TIEARRAY {bless[]}
1132sub STORE{}
1133sub FETCH { 72 }
1134tie my @x, "main";
1135my $elem = \$x[0];
1136$$elem = *bar;
1137print &{\&$$elem}, "\n";
1138EXPECT
113973
1140########
1141
1142# \&$tied with $tied holding a PVGV glob before the fetch (but not after)
1143local *72 = sub { 73 };
1144sub TIESCALAR {bless[]}
1145sub STORE{}
1146sub FETCH { 72 }
1147tie my $x, "main";
1148$x = *bar;
1149print &{\&$x}, "\n";
1150EXPECT
115173
1152########
1153
1154# Lexicals should not be visible to magic methods on scope exit
1155BEGIN { unless (defined &DynaLoader::boot_DynaLoader) {
1156    print "HASH\nHASH\nARRAY\nARRAY\n"; exit;
1157}}
1158use Scalar::Util 'weaken';
1159{ package xoufghd;
1160  sub TIEHASH { Scalar::Util::weaken($_[1]); bless \$_[1], xoufghd:: }
1161  *TIEARRAY = *TIEHASH;
1162  DESTROY {
1163     bless ${$_[0]} || return, 0;
1164} }
1165for my $sub (
1166    # hashes: ties before backrefs
1167    sub {
1168        my %hash;
1169        $ref = ref \%hash;
1170        tie %hash, xoufghd::, \%hash;
1171        1;
1172    },
1173    # hashes: backrefs before ties
1174    sub {
1175        my %hash;
1176        $ref = ref \%hash;
1177        weaken(my $x = \%hash);
1178        tie %hash, xoufghd::, \%hash;
1179        1;
1180    },
1181    # arrays: ties before backrefs
1182    sub {
1183        my @array;
1184        $ref = ref \@array;
1185        tie @array, xoufghd::, \@array;
1186        1;
1187    },
1188    # arrays: backrefs before ties
1189    sub {
1190        my @array;
1191        $ref = ref \@array;
1192        weaken(my $x = \@array);
1193        tie @array, xoufghd::, \@array;
1194        1;
1195    },
1196) {
1197    &$sub;
1198    &$sub;
1199    print $ref, "\n";
1200}
1201EXPECT
1202HASH
1203HASH
1204ARRAY
1205ARRAY
1206########
1207
1208# Localising a tied variable with a typeglob in it should copy magic
1209sub TIESCALAR{bless[]}
1210sub FETCH{warn "fetching\n"; *foo}
1211sub STORE{}
1212tie $x, "";
1213local $x;
1214warn "before";
1215"$x";
1216warn "after";
1217EXPECT
1218fetching
1219before at - line 8.
1220fetching
1221after at - line 10.
1222########
1223
1224# tied returns same value as tie
1225sub TIESCALAR{bless[]}
1226$tyre = \tie $tied, "";
1227print "ok\n" if \tied $tied == $tyre;
1228EXPECT
1229ok
1230########
1231
1232# tied arrays should always be AvREAL
1233$^W=1;
1234sub TIEARRAY{bless[]}
1235sub {
1236  tie @_, "";
1237  \@_; # used to produce: av_reify called on tied array at - line 7.
1238}->(1);
1239EXPECT
1240########
1241
1242# [perl #67490] scalar-tying elements of magic hashes
1243sub TIESCALAR{bless[]}
1244sub STORE{}
1245tie $ENV{foo}, '';
1246$ENV{foo} = 78;
1247delete $ENV{foo};
1248tie $^H{foo}, '';
1249$^H{foo} = 78;
1250delete $^H{foo};
1251EXPECT
1252########
1253
1254# [perl #35865, #43011] autovivification should call FETCH after STORE
1255# because perl does not know that the FETCH would have returned the same
1256# thing that was just stored.
1257
1258# This package never likes to take ownership of other people’s refs.  It
1259# always makes its own copies.  (For simplicity, it only accepts hashes.)
1260package copier {
1261    sub TIEHASH { bless {} }
1262    sub FETCH   { $_[0]{$_[1]} }
1263    sub STORE   { $_[0]{$_[1]} = { %{ $_[2] } } }
1264}
1265tie my %h, copier::;
1266$h{i}{j} = 'k';
1267print $h{i}{j}, "\n";
1268EXPECT
1269k
1270########
1271
1272# [perl #8931] FETCH for tied $" called an odd number of times.
1273use strict;
1274my $i = 0;
1275sub A::TIESCALAR {bless [] => 'A'}
1276sub A::FETCH {print ++ $i, "\n"}
1277my @a = ("", "", "");
1278
1279tie $" => 'A';
1280"@a";
1281
1282$i = 0;
1283tie my $a => 'A';
1284join $a, 1..10;
1285EXPECT
12861
12871
1288########
1289
1290# [perl #9391] return value from 'tied' not discarded soon enough
1291use warnings;
1292tie @a, 'T';
1293if (tied @a) {
1294untie @a;
1295}
1296
1297sub T::TIEARRAY { my $s; bless \$s => "T" }
1298EXPECT
1299########
1300
1301# NAME Test that tying a hash does not leak a deleted iterator
1302# This produced unbalanced string table warnings under
1303# PERL_DESTRUCT_LEVEL=2.
1304package l {
1305    sub TIEHASH{bless[]}
1306}
1307$h = {foo=>0};
1308each %$h;
1309delete $$h{foo};
1310tie %$h, 'l';
1311EXPECT
1312########
1313
1314# NAME EXISTS on arrays
1315sub TIEARRAY{bless[]};
1316sub FETCHSIZE { 50 }
1317sub EXISTS { print "does $_[1] exist?\n" }
1318tie @a, "";
1319exists $a[1];
1320exists $a[-1];
1321$NEGATIVE_INDICES=1;
1322exists $a[-1];
1323EXPECT
1324does 1 exist?
1325does 49 exist?
1326does -1 exist?
1327########
1328
1329# Crash when using negative index on array tied to non-object
1330sub TIEARRAY{bless[]};
1331${\tie @a, ""} = undef;
1332eval { $_ = $a[-1] }; print $@;
1333eval { $a[-1] = '' }; print $@;
1334eval { delete $a[-1] }; print $@;
1335eval { exists $a[-1] }; print $@;
1336
1337EXPECT
1338Can't call method "FETCHSIZE" on an undefined value at - line 5.
1339Can't call method "FETCHSIZE" on an undefined value at - line 6.
1340Can't call method "FETCHSIZE" on an undefined value at - line 7.
1341Can't call method "FETCHSIZE" on an undefined value at - line 8.
1342########
1343
1344# Crash when reading negative index when NEGATIVE_INDICES stub exists
1345sub NEGATIVE_INDICES;
1346sub TIEARRAY{bless[]};
1347sub FETCHSIZE{}
1348tie @a, "";
1349print "ok\n" if ! defined $a[-1];
1350EXPECT
1351ok
1352########
1353
1354# Assigning vstrings to tied scalars
1355sub TIESCALAR{bless[]};
1356sub STORE { print ref \$_[1], "\n" }
1357tie $x, ""; $x = v3;
1358EXPECT
1359VSTRING
1360########
1361
1362# [perl #27010] Tying deferred elements
1363$\="\n";
1364sub TIESCALAR{bless[]};
1365sub {
1366    tie $_[0], "";
1367    print ref tied $h{k};
1368    tie $h{l}, "";
1369    print ref tied $_[1];
1370    untie $h{k};
1371    print tied $_[0] // 'undef';
1372    untie $_[1];
1373    print tied $h{l} // 'undef';
1374    # check that tied and untie do not autovivify
1375    # XXX should they autovivify?
1376    tied $_[2];
1377    print exists $h{m} ? "yes" : "no";
1378    untie $_[2];
1379    print exists $h{m} ? "yes" : "no";
1380}->($h{k}, $h{l}, $h{m});
1381EXPECT
1382main
1383main
1384undef
1385undef
1386no
1387no
1388########
1389
1390# [perl #78194] Passing op return values to tie constructors
1391sub TIEARRAY{
1392    print \$_[1] == \$_[1] ? "ok\n" : "not ok\n";
1393};
1394tie @a, "", "$a$b";
1395EXPECT
1396ok
1397########
1398
1399# Scalar-tied locked hash keys and copy-on-write
1400use Tie::Scalar;
1401tie $h{foo}, Tie::StdScalar;
1402tie $h{bar}, Tie::StdScalar;
1403$h{foo} = __PACKAGE__; # COW
1404$h{bar} = 1;       # not COW
1405# Moral equivalent of Hash::Util::lock_whatever, but miniperl-compatible
1406Internals::SvREADONLY($h{foo},1);
1407Internals::SvREADONLY($h{bar},1);
1408print $h{foo}, "\n"; # should not croak
1409# Whether the value is COW should make no difference here (whether the
1410# behaviour is ultimately correct is another matter):
1411local $h{foo};
1412local $h{bar};
1413print "ok\n" if (eval{ $h{foo} = 1 }||$@) eq (eval{ $h{bar} = 1 }||$@);
1414EXPECT
1415main
1416ok
1417########
1418
1419# &xsub and goto &xsub with tied @_
1420use Tie::Array;
1421tie @_, Tie::StdArray;
1422@_ = "\xff";
1423&utf8::encode;
1424printf "%x\n", $_ for map ord, split //, $_[0];
1425print "--\n";
1426@_ = "\xff";
1427& {sub { goto &utf8::encode }};
1428printf "%x\n", $_ for map ord, split //, $_[0];
1429EXPECT
1430c3
1431bf
1432--
1433c3
1434bf
1435########
1436
1437# Defelem pointing to nonexistent element of tied array
1438
1439use Tie::Array;
1440# This sub is called with a deferred element.  Inside the sub, $_[0] pros-
1441# pectively points to element 10000 of @a.
1442sub {
1443  tie @a, "Tie::StdArray";  # now @a is tied
1444  $#a = 20000;  # and FETCHSIZE/AvFILL will now return a big number
1445  $a[10000] = "crumpets\n";
1446  $_ = "$_[0]"; # but defelems don’t expect tied arrays and try to read
1447                # AvARRAY[10000], which crashes
1448}->($a[10000]);
1449print
1450EXPECT
1451crumpets
1452