3 # Add new tests to the end with format:
9 # Warn or die msgs (if any) at - line 1234
18 run_multiple_progs('', \*DATA);
24 # standard behaviour, without any extra references
30 # SKIP ?!defined &DynaLoader::boot_DynaLoader && !eval 'require base'
31 # (skip under miniperl if base.pm is not in lib/ yet)
33 # standard behaviour, without any extra references
35 {package Tie::HashUntie;
36 use base 'Tie::StdHash';
42 tie %h, Tie::HashUntie;
48 # standard behaviour, with 1 extra reference
50 $a = tie %h, Tie::StdHash;
55 # standard behaviour, with 1 extra reference via tied
63 # standard behaviour, with 1 extra reference which is destroyed
65 $a = tie %h, Tie::StdHash;
71 # standard behaviour, with 1 extra reference via tied which is destroyed
80 # strict behaviour, without any extra references
88 # strict behaviour, with 1 extra references generating an error
91 $a = tie %h, Tie::StdHash;
94 untie attempted while 1 inner references still exist at - line 6.
97 # strict behaviour, with 1 extra references via tied generating an error
100 tie %h, Tie::StdHash;
104 untie attempted while 1 inner references still exist at - line 7.
107 # strict behaviour, with 1 extra references which are destroyed
108 use warnings 'untie';
110 $a = tie %h, Tie::StdHash;
116 # strict behaviour, with extra 1 references via tied which are destroyed
117 use warnings 'untie';
119 tie %h, Tie::StdHash;
126 # strict error behaviour, with 2 extra references
127 use warnings 'untie';
129 $a = tie %h, Tie::StdHash;
133 untie attempted while 2 inner references still exist at - line 7.
136 # strict behaviour, check scope of strictness.
139 $A = tie %H, Tie::StdHash;
142 use warnings 'untie';
144 tie %h, Tie::StdHash;
151 # Forbidden aggregate self-ties
152 sub Self::TIEHASH { bless $_[1], $_[0] }
158 Self-ties of arrays and hashes are not supported at - line 6.
161 # Allowed scalar self-ties
163 sub Self::TIESCALAR { bless $_[1], $_[0] }
164 sub Self::DESTROY { $destroyed = 1; }
169 die "self-tied scalar not DESTROYed" unless $destroyed == 1;
173 # Allowed glob self-ties
176 sub Self2::TIEHANDLE { bless $_[1], $_[0] }
177 sub Self2::DESTROY { $destroyed = 1; }
178 sub Self2::PRINT { $printed = 1; }
182 tie *$c, 'Self2', $c;
185 die "self-tied glob not PRINTed" unless $printed == 1;
186 die "self-tied glob not DESTROYed" unless $destroyed == 1;
190 # Allowed IO self-ties
192 sub Self3::TIEHANDLE { bless $_[1], $_[0] }
193 sub Self3::DESTROY { $destroyed = 1; }
194 sub Self3::PRINT { $printed = 1; }
196 use Symbol 'geniosym';
198 tie *$c, 'Self3', $c;
201 die "self-tied IO not PRINTed" unless $printed == 1;
202 die "self-tied IO not DESTROYed" unless $destroyed == 1;
206 # TODO IO "self-tie" via TEMP glob
208 sub Self3::TIEHANDLE { bless $_[1], $_[0] }
209 sub Self3::DESTROY { $destroyed = 1; }
210 sub Self3::PRINT { $printed = 1; }
212 use Symbol 'geniosym';
214 tie *$c, 'Self3', \*$c;
217 die "IO tied to TEMP glob not PRINTed" unless $printed == 1;
218 die "IO tied to TEMP glob not DESTROYed" unless $destroyed == 1;
222 # Interaction of tie and vec
226 tie $a,Tie::StdScalar or die;
235 # correct unlocalisation of tied hashes (patch #16431)
237 tie %tied, Tie::StdHash;
238 { local $hash{'foo'} } warn "plain hash bad unlocalize" if exists $hash{'foo'};
239 { local $tied{'foo'} } warn "tied hash bad unlocalize" if exists $tied{'foo'};
240 { local $ENV{'foo'} } warn "%ENV bad unlocalize" if exists $ENV{'foo'};
244 # An attempt at lvalueable barewords broke this
247 Can't modify constant item in tie at - line 3, near "'main';"
248 Execution of - aborted due to compilation errors.
251 # localizing tied hash slices
254 print exists $ENV{FooA} ? 1 : 0, "\n";
255 print exists $ENV{FooB} ? 2 : 0, "\n";
256 print exists $ENV{FooC} ? 3 : 0, "\n";
258 local @ENV{qw(FooA FooC)};
259 print exists $ENV{FooA} ? 4 : 0, "\n";
260 print exists $ENV{FooB} ? 5 : 0, "\n";
261 print exists $ENV{FooC} ? 6 : 0, "\n";
263 print exists $ENV{FooA} ? 7 : 0, "\n";
264 print exists $ENV{FooB} ? 8 : 0, "\n";
265 print exists $ENV{FooC} ? 9 : 0, "\n"; # this should not exist
278 # FETCH freeing tie'd SV still works
279 sub TIESCALAR { bless [] }
280 sub FETCH { *a = \1; 2 }
287 # [20020716.007 (#10080)] - nested FETCHES
289 sub F1::TIEARRAY { bless [], 'F1' }
294 sub F2::TIEARRAY { bless [2], 'F2' }
295 sub F2::FETCH { my $self = shift; my $x = $f1[3]; $self }
299 print $f2[4][0],"\n";
301 sub F3::TIEHASH { bless [], 'F3' }
306 sub F4::TIEHASH { bless [3], 'F4' }
307 sub F4::FETCH { my $self = shift; my $x = $f3{3}; $self }
311 print $f4{'foo'}[0],"\n";
317 # test untie() from within FETCH
319 sub TIESCALAR { my $pkg = shift; return bless [@_], $pkg; }
322 my ($obj, $field) = @$self;
323 untie $obj->{$field};
324 $obj->{$field} = "Bar";
327 tie $a->{foo}, "Foo", $a, "foo";
328 my $s = $a->{foo}; # access once
329 # the hash element should not be tied anymore
330 print defined tied $a->{foo} ? "not ok" : "ok";
334 # the tmps returned by FETCH should appear to be SCALAR
335 # (even though they are now implemented using PVLVs.)
337 sub TIEHASH { bless {} }
338 sub TIEARRAY { bless {} }
345 my $s = "$r1 ". ref($r1) . " $r2 " . ref($r2);
349 SCALAR SCALAR SCALAR SCALAR
351 # [perl #23287] segfault in untie
352 sub TIESCALAR { bless $_[1], $_[0] }
354 tie $var, 'main', \$var;
358 # Test case from perlmonks by runrig
359 # http://www.perlmonks.org/index.pl?node_id=273490
360 # "Here is what I tried. I think its similar to what you've tried
361 # above. Its odd but convenient that after untie'ing you are left with
362 # a variable that has the same value as was last returned from
363 # FETCH. (At least on my perl v5.6.1). So you don't need to pass a
364 # reference to the variable in order to set it after the untie (here it
365 # is accessed through a closure)."
370 my ($class,$code) = @_;
380 tie $var, 'MyTied', sub { untie $var; 4 };
396 # [perl #22297] cannot untie scalar from within tied FETCH
400 tie $x, 'Overlay', $ref, $x;
406 #print "WILL EXTERNAL UNTIE $ref\n";
412 #print "counter = $counter\n";
414 print (($counter == 1) ? "ok\n" : "not ok\n");
421 my ($ref, $val) = @_;
422 return bless [ $ref, $val ], $pkg;
428 my ($ref, $val) = @$self;
429 #print "WILL INTERNAL UNITE $ref\n";
438 # [perl #948] cannot meaningfully tie $,
439 package TieDollarComma;
443 return bless \my $x, $pkg;
449 print "STORE set '$$self'\n";
459 tie $,, 'TieDollarComma';
461 print "join", "things", "up\n";
464 join<FETCH>BOBBINSthings<FETCH>BOBBINSup
476 $_[0]->{$_[1]} = $_[2];
489 return 0 if ! keys %{$_[0]};
490 sprintf "%i/%i", scalar keys %{$_[0]}, scalar keys %{$_[0]};
494 tie my %h => "TieScalar";
497 print scalar %h, "\n"
498 if %h; # this should also call SCALAR but implicitly
500 print scalar %h, "\n"
501 if !%h; # this should also call SCALAR but implicitly
511 # test scalar on tied hash when no SCALAR method has been given
519 $_[0]->{$_[1]} = $_[2];
528 my $a = keys %{ $_[0] };
534 tie my %h => "TieScalar";
543 print "not empty\n" if %h;
544 print "not empty\n" if %h;
546 my ($k,$v) = each %h;
548 print "not empty\n" if %h;
550 print "empty\n" if ! %h;
565 sub TIESCALAR { bless {} }
566 sub FETCH { my $x = 3.3; 1 if 0+$x; $x }
572 sub TIESCALAR { bless {} }
573 sub FETCH { shift()->{i} ++ }
580 # skipped on EBCDIC because "2" | "8" is 0xFA (not COLON as it is on ASCII),
581 # which isn't representable in this file's UTF-8 encoding.
582 # Bug 53482 (and maybe others)
584 sub TIESCALAR { my $foo = $_[1]; bless \$foo, $_[0] }
585 sub FETCH { ${$_[0]} }
586 tie my $x1, "main", 2;
587 tie my $y1, "main", 8;
590 tie my $x2, "main", "2";
591 tie my $y2, "main", "8";
598 sub TIEHASH { bless {}, $_[0] }
599 sub STORE { $_[0]->{$_[1]} = $_[2] }
600 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
601 sub NEXTKEY { each %{$_[0]} }
602 sub DELETE { delete $_[0]->{$_[1]} }
603 sub CLEAR { %{$_[0]} = () }
606 print scalar keys %h, "\n";
611 print scalar keys %h, "\n";
617 sub foo::TIESCALAR { bless {value => $_[1]}, $_[0] }
618 sub foo::FETCH { $_[0]->{value} }
619 tie my $VAR, 'foo', '42';
620 foreach my $var ($VAR) {
621 print +($var eq $VAR) ? "yes\n" : "no\n";
626 sub TIEARRAY { bless [], 'main' }
631 print "tied\n" if tied @a;
634 sub TIEHASH { bless [], 'main' }
639 print "tied\n" if tied %h;
642 # RT 20727: PL_defoutgv is left as a tied element
643 sub TIESCALAR { return bless {}, 'main' }
648 select(); # this used to coredump or assert fail
654 # RT 23810: eval in die in FETCH can corrupt context stack
656 my $file = 'rt23810.pm';
662 my ($str, $eval) = @_;
663 open my $fh, '>', $file or die "Can't create $file: $!\n";
668 eval { require $pm; $s .= '-ENDE' }
678 sub TIEHASH { bless {} }
681 # 10 or more syntax errors makes yyparse croak()
682 my $bad = q{$x+;$x+;$x+;$x+;$x+;$x+;$x+;$x+;$x+$x+;$x+;$x+;$x+;$x+;;$x+;};
684 if ($_[1] eq 'eval') {
686 eval q[BEGIN { die; $s .= '-X1' }];
688 eval q[BEGIN { $x+ }];
692 $s .= '-S1' while $@ =~ /syntax error at/g;
695 $s .= '-S2' while $@ =~ /syntax error at/g;
697 elsif ($_[1] eq 'require') {
700 q[BEGIN { die; $s .= '-X1' }],
705 for my $i (0..$#text) {
707 do_require($txt[$i], 0) if $e;;
708 do_require($txt[$i], 1);
711 elsif ($_[1] eq 'exit') {
712 eval q[exit(0); print "overshot eval\n"];
715 print "unknown key: '$_[1]'\n";
722 for my $action(qw(eval require)) {
723 $s = ''; $e = 0; $s .= main->FETCH($action); print "$action: s0=$s\n";
724 $s = ''; $e = 1; eval { $s .= main->FETCH($action)}; print "$action: s1=$s\n";
725 $s = ''; $e = 0; $s .= $foo{$action}; print "$action: s2=$s\n";
726 $s = ''; $e = 1; eval { $s .= $foo{$action}}; print "$action: s3=$s\n";
728 1 while unlink $file;
731 print "overshot main\n"; # shouldn't reach here
734 eval: s0=EVAL-BD-BS-E1-S1-E2-S2-R
735 eval: s1=EVAL-BD-BS-E1-S1-E2-S2-R
736 eval: s2=EVAL-BD-BS-E1-S1-E2-S2-R
737 eval: s3=EVAL-BD-BS-E1-S1-E2-S2-R
738 require: s0=REQUIRE-0-ERQ-ENDRQ-1-ERQ-ENDRQ-2-ERQ-ENDRQ-3-ERQ-ENDRQ-R
739 require: s1=REQUIRE-0-RQ
740 require: s2=REQUIRE-0-ERQ-ENDRQ-1-ERQ-ENDRQ-2-ERQ-ENDRQ-3-ERQ-ENDRQ-R
741 require: s3=REQUIRE-0-RQ
743 # RT 8857: STORE incorrectly invoked for local($_) on aliased tied array
746 sub TIEARRAY { bless [], $_[0] }
747 sub TIEHASH { bless [], $_[0] }
748 sub FETCH { $_[0]->[$_[1]] }
749 sub STORE { $_[0]->[$_[1]] = $_[2] }
758 foreach ($a[0], $h{a}) {
761 # on failure, chucks up 'premature free' etc messages
765 # the initial fix for this bug caused tied scalar FETCH to be called
766 # multiple times when that scalar was an element in an array. Check it
767 # only gets called once now.
769 sub TIESCALAR { bless [], $_[0] }
771 sub FETCH { $c++; 0 }
773 sub STORE { $c += 100; 0 }
781 my $x = $a[0] + $h{foo} + $a[$i] + (@a)[0];
786 # Bug 68192 - numeric ops not calling mg_get when tied scalar holds a ref
787 sub TIESCALAR { bless {}, __PACKAGE__ };
790 print "fetching... "; # make sure FETCH is called once per op
794 tie $foo, __PACKAGE__;
797 print "+ ", 0 + $foo, "\n";
798 print "** ", $foo**1, "\n";
799 print "* ", $foo*1, "\n";
800 print "/ ", $foo*1, "\n";
801 print "% ", $foo%123457, "\n";
802 print "- ", $foo-0, "\n";
803 print "neg ", - -$foo, "\n";
804 print "int ", int $foo, "\n";
805 print "abs ", abs $foo, "\n";
806 print "== ", 123456 == $foo, "\n";
807 print "< ", 123455 < $foo, "\n";
808 print "> ", 123457 > $foo, "\n";
809 print "<= ", 123456 <= $foo, "\n";
810 print ">= ", 123456 >= $foo, "\n";
811 print "!= ", 0 != $foo, "\n";
812 print "<=> ", 123457 <=> $foo, "\n";
815 fetching... ** 123456
820 fetching... neg 123456
821 fetching... int 123456
822 fetching... abs 123456
831 # Ties returning overloaded objects
835 '*{}' => sub { print '*{}'; \*100 },
836 '@{}' => sub { print '@{}'; \@100 },
837 '%{}' => sub { print '%{}'; \%100 },
838 '${}' => sub { print '${}'; \$100 },
841 $_ => sub { print "$op"; 100 }
842 } qw< 0+ "" + ** * / % - neg int abs == < > <= >= != <=> <> >
844 $o = bless [], overloaded;
846 sub TIESCALAR { bless {}, "" }
847 sub FETCH { print "fetching... "; $o }
851 $ghew=undef; 1+$ghew; print "\n";
852 $ghew=undef; $ghew**1; print "\n";
853 $ghew=undef; $ghew*1; print "\n";
854 $ghew=undef; $ghew/1; print "\n";
855 $ghew=undef; $ghew%1; print "\n";
856 $ghew=undef; $ghew-1; print "\n";
857 $ghew=undef; -$ghew; print "\n";
858 $ghew=undef; int $ghew; print "\n";
859 $ghew=undef; abs $ghew; print "\n";
860 $ghew=undef; 1 == $ghew; print "\n";
861 $ghew=undef; $ghew<1; print "\n";
862 $ghew=undef; $ghew>1; print "\n";
863 $ghew=undef; $ghew<=1; print "\n";
864 $ghew=undef; $ghew >=1; print "\n";
865 $ghew=undef; $ghew != 1; print "\n";
866 $ghew=undef; $ghew<=>1; print "\n";
867 $ghew=undef; <$ghew>; print "\n";
868 $ghew=\*shrext; *$ghew; print "\n";
869 $ghew=\@spled; @$ghew; print "\n";
870 $ghew=\%frit; %$ghew; print "\n";
871 $ghew=\$drile; $$ghew; print "\n";
895 # RT 51636: segmentation fault with array ties
899 print "ok\n"; # if we got here we didn't crash
903 sub TIEARRAY { bless {} }
904 sub STORE { tie my @b, 'T' }
911 # RT 8438: Tied scalars don't call FETCH when subref is dereferenced
913 sub TIESCALAR { bless {} }
917 sub FETCH { $fetch++; sub { $called++ } }
921 print "fetch=$fetch\ncalled=$called\n";
927 # tie mustn't attempt to call methods on bareword filehandles.
928 sub IO::File::TIEARRAY {
929 die "Did not want to invoke IO::File::TIEARRAY";
931 fileno FOO; tie @a, "FOO"
933 Can't locate object method "TIEARRAY" via package "FOO" (perhaps you forgot to load "FOO"?) at - line 5.
935 # tie into empty package name
938 Can't locate object method "TIESCALAR" via package "main" at - line 2.
940 # tie into undef package name
943 Can't locate object method "TIESCALAR" via package "main" at - line 2.
945 # tie into nonexistent glob [RT#130623 assertion failure]
948 Can't locate object method "TIESCALAR" via package "FOO" at - line 2.
950 # tie into glob when package exists but not method: no "*", no "main::"
951 { package PackageWithoutTIESCALAR }
952 tie $foo, *PackageWithoutTIESCALAR;
954 Can't locate object method "TIESCALAR" via package "PackageWithoutTIESCALAR" at - line 3.
956 # tie into reference [RT#130623 assertion failure]
957 eval { tie $foo, \"nope" };
959 print $exn =~ s/0x\w+/0xNNN/rg;
961 Can't locate object method "TIESCALAR" via package "SCALAR(0xNNN)" at - line 2.
964 # STORE freeing tie'd AV
965 sub TIEARRAY { bless [] }
966 sub STORE { *a = []; 1 }
974 # CLEAR freeing tie'd AV
975 sub TIEARRAY { bless [] }
976 sub CLEAR { *a = []; 1 }
985 # FETCHSIZE freeing tie'd AV
986 sub TIEARRAY { bless [] }
987 sub FETCHSIZE { *a = []; 100 }
997 # [perl #86328] Crash when freeing tie magic that can increment the refcnt
999 no warnings 'experimental::builtin';
1000 use builtin 'weaken';
1005 *TIEARRAY = *TIEHASH;
1014 weaken($o->[0] = $a);
1015 tie %$a, "main", $o;
1019 weaken($p->[0] = $b);
1020 tie @$b, "main", $p;
1022 # Done setting up the evil data structures
1032 # Localising a tied COW scalar should not make it read-only.
1034 sub TIESCALAR { bless [] }
1035 sub FETCH { __PACKAGE__ }
1048 # Nor should it be impossible to tie COW scalars that are already PVMGs.
1050 sub TIESCALAR { bless [] }
1052 undef $x; # downgrade to PVMG
1053 $x = __PACKAGE__; # PVMG + COW
1056 print STDERR "ok\n";
1058 # However, one should not be able to tie read-only glob copies, which look
1059 # a bit like kine internally (FAKE + READONLY).
1061 Internals::SvREADONLY($y,1);
1066 Modification of a read-only value attempted at - line 16.
1069 # And one should not be able to tie read-only COWs
1070 for(__PACKAGE__) { tie $_, "" }
1071 sub TIESCALAR {bless []}
1073 Modification of a read-only value attempted at - line 3.
1076 # Similarly, read-only regexps cannot be tied.
1077 sub TIESCALAR { bless [] }
1079 Internals::SvREADONLY($y,1);
1083 Modification of a read-only value attempted at - line 6.
1086 # tied() should still work on tied scalars after glob assignment
1087 sub TIESCALAR {bless[]}
1089 sub f::TIEHANDLE{bless[],f}
1092 [$rin]; # call FETCH
1093 print ref tied $rin, "\n";
1094 print ref tied *$rin, "\n";
1100 # (un)tie $glob_copy vs (un)tie *$glob_copy
1101 sub TIESCALAR { print "TIESCALAR\n"; bless [] }
1102 sub TIEHANDLE{ print "TIEHANDLE\n"; bless [] }
1103 sub FETCH { print "never called\n" }
1108 print "ok 1\n" if !tied $f;
1109 () = $f; # should not call FETCH
1111 print "ok 2\n" if !tied *foo;
1119 # RT #8611 mustn't goto outside the magic stack
1120 sub TIESCALAR { warn "tiescalar\n"; bless [] }
1121 sub FETCH { warn "fetch()\n"; goto FOO; }
1123 warn "before fetch\n";
1125 warn "before FOO\n";
1132 Can't find label FOO at - line 4.
1135 # RT #8611 mustn't goto outside the magic stack
1136 sub TIEHANDLE { warn "tiehandle\n"; bless [] }
1137 sub PRINT { warn "print()\n"; goto FOO; }
1139 warn "before print\n";
1141 warn "before FOO\n";
1148 Can't find label FOO at - line 4.
1151 # \&$tied with $tied holding a reference before the fetch (but not after)
1153 sub TIESCALAR {bless[]}
1164 # \&$tied with $tied holding a PVLV glob before the fetch (but not after)
1166 sub TIEARRAY {bless[]}
1172 print &{\&$$elem}, "\n";
1177 # \&$tied with $tied holding a PVGV glob before the fetch (but not after)
1178 local *72 = sub { 73 };
1179 sub TIESCALAR {bless[]}
1184 print &{\&$x}, "\n";
1189 # Lexicals should not be visible to magic methods on scope exit
1190 BEGIN { unless (defined &DynaLoader::boot_DynaLoader) {
1191 print "HASH\nHASH\nARRAY\nARRAY\n"; exit;
1193 no warnings 'experimental::builtin';
1194 use builtin 'weaken';
1196 sub TIEHASH { weaken($_[1]); bless \$_[1], xoufghd:: }
1197 *TIEARRAY = *TIEHASH;
1199 bless ${$_[0]} || return, 0;
1202 # hashes: ties before backrefs
1206 tie %hash, xoufghd::, \%hash;
1209 # hashes: backrefs before ties
1213 weaken(my $x = \%hash);
1214 tie %hash, xoufghd::, \%hash;
1217 # arrays: ties before backrefs
1221 tie @array, xoufghd::, \@array;
1224 # arrays: backrefs before ties
1228 weaken(my $x = \@array);
1229 tie @array, xoufghd::, \@array;
1244 # Localising a tied variable with a typeglob in it should copy magic
1245 sub TIESCALAR{bless[]}
1246 sub FETCH{warn "fetching\n"; *foo}
1260 # tied returns same value as tie
1261 sub TIESCALAR{bless[]}
1262 $tyre = \tie $tied, "";
1263 print "ok\n" if \tied $tied == $tyre;
1268 # tied arrays should always be AvREAL
1270 sub TIEARRAY{bless[]}
1273 \@_; # used to produce: av_reify called on tied array at - line 7.
1278 # [perl #67490] scalar-tying elements of magic hashes
1279 sub TIESCALAR{bless[]}
1290 # [perl #35865, #43011] autovivification should call FETCH after STORE
1291 # because perl does not know that the FETCH would have returned the same
1292 # thing that was just stored.
1294 # This package never likes to take ownership of other people’s refs. It
1295 # always makes its own copies. (For simplicity, it only accepts hashes.)
1297 sub TIEHASH { bless {} }
1298 sub FETCH { $_[0]{$_[1]} }
1299 sub STORE { $_[0]{$_[1]} = { %{ $_[2] } } }
1301 tie my %h, copier::;
1303 print $h{i}{j}, "\n";
1308 # [perl #8931] FETCH for tied $" called an odd number of times.
1311 sub A::TIESCALAR {bless [] => 'A'}
1312 sub A::FETCH {print ++ $i, "\n"}
1313 my @a = ("", "", "");
1326 # [perl #9391] return value from 'tied' not discarded soon enough
1333 sub T::TIEARRAY { my $s; bless \$s => "T" }
1337 # NAME Test that tying a hash does not leak a deleted iterator
1338 # This produced unbalanced string table warnings under
1339 # PERL_DESTRUCT_LEVEL=2.
1341 sub TIEHASH{bless[]}
1350 # NAME EXISTS on arrays
1351 sub TIEARRAY{bless[]};
1352 sub FETCHSIZE { 50 }
1353 sub EXISTS { print "does $_[1] exist?\n" }
1357 $NEGATIVE_INDICES=1;
1365 # Crash when using negative index on array tied to non-object
1366 sub TIEARRAY{bless[]};
1367 ${\tie @a, ""} = undef;
1368 eval { $_ = $a[-1] }; print $@;
1369 eval { $a[-1] = '' }; print $@;
1370 eval { delete $a[-1] }; print $@;
1371 eval { exists $a[-1] }; print $@;
1374 Can't call method "FETCHSIZE" on an undefined value at - line 5.
1375 Can't call method "FETCHSIZE" on an undefined value at - line 6.
1376 Can't call method "FETCHSIZE" on an undefined value at - line 7.
1377 Can't call method "FETCHSIZE" on an undefined value at - line 8.
1380 # Crash when reading negative index when NEGATIVE_INDICES stub exists
1381 sub NEGATIVE_INDICES;
1382 sub TIEARRAY{bless[]};
1385 print "ok\n" if ! defined $a[-1];
1390 # Assigning vstrings to tied scalars
1391 sub TIESCALAR{bless[]};
1392 sub STORE { print ref \$_[1], "\n" }
1393 tie $x, ""; $x = v3;
1398 # [perl #27010] Tying deferred elements
1400 sub TIESCALAR{bless[]};
1403 print ref tied $h{k};
1405 print ref tied $_[1];
1407 print tied $_[0] // 'undef';
1409 print tied $h{l} // 'undef';
1410 # check that tied and untie do not autovivify
1411 # XXX should they autovivify?
1413 print exists $h{m} ? "yes" : "no";
1415 print exists $h{m} ? "yes" : "no";
1416 }->($h{k}, $h{l}, $h{m});
1426 # [perl #78194] Passing op return values to tie constructors
1428 print \$_[1] == \$_[1] ? "ok\n" : "not ok\n";
1435 # Scalar-tied locked hash keys and copy-on-write
1437 tie $h{foo}, Tie::StdScalar;
1438 tie $h{bar}, Tie::StdScalar;
1439 $h{foo} = __PACKAGE__; # COW
1440 $h{bar} = 1; # not COW
1441 # Moral equivalent of Hash::Util::lock_whatever, but miniperl-compatible
1442 Internals::SvREADONLY($h{foo},1);
1443 Internals::SvREADONLY($h{bar},1);
1444 print $h{foo}, "\n"; # should not croak
1445 # Whether the value is COW should make no difference here (whether the
1446 # behaviour is ultimately correct is another matter):
1449 print "ok\n" if (eval{ $h{foo} = 1 }||$@) eq (eval{ $h{bar} = 1 }||$@);
1454 # SKIP ? $::IS_EBCDIC
1455 # skipped on EBCDIC because different from ASCII and results vary depending on
1458 # &xsub and goto &xsub with tied @_
1460 tie @_, Tie::StdArray;
1463 printf "%x\n", $_ for map ord, split //, $_[0];
1466 & {sub { goto &utf8::encode }};
1467 printf "%x\n", $_ for map ord, split //, $_[0];
1476 # Defelem pointing to nonexistent element of tied array
1479 # This sub is called with a deferred element. Inside the sub, $_[0] pros-
1480 # pectively points to element 10000 of @a.
1482 tie @a, "Tie::StdArray"; # now @a is tied
1483 $#a = 20000; # and FETCHSIZE/AvFILL will now return a big number
1484 $a[10000] = "crumpets\n";
1485 $_ = "$_[0]"; # but defelems don't expect tied arrays and try to read
1486 # AvARRAY[10000], which crashes
1493 # tied() in list assignment
1495 sub TIESCALAR : lvalue {
1496 ${+pop} = bless [], shift;
1500 ($a, $b) = (3, tied $t);
1507 # when assigning to array/hash, ensure get magic is processed first
1510 tie %tied, "Tie::StdHash";
1512 my @a = values %tied;
1513 %tied = qw(b bar); # overwrites @a's contents unless magic was called
1515 my %h = ("x", values %tied);
1516 %tied = qw(c baz); # overwrites @a's contents unless magic was called
1523 # keys(%tied) in bool context without SCALAR present
1525 my %inner = (a =>1, b => 2, c => 3);
1526 sub TIEHASH { bless \%inner, $_[0] }
1527 sub FIRSTKEY { $f++; my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1528 sub NEXTKEY { $n++; each %{$_[0]} }
1531 print "[$x][$f][$n]\n";
1534 print "[$x][$f][$n]\n";
1539 # keys(%tied) in bool context with SCALAR present
1540 my ($f,$n, $s) = (0,0,0);
1541 my %inner = (a =>1, b => 2, c => 3);
1542 sub TIEHASH { bless \%inner, $_[0] }
1543 sub FIRSTKEY { $f++; my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1544 sub NEXTKEY { $n++; each %{$_[0]} }
1545 sub SCALAR { $s++; scalar %{$_[0]} }
1548 print "[$x][$f][$n][$s]\n";
1551 print "[$x][$f][$n][$s]\n";
1556 # keys(%tied) in scalar context without SCALAR present
1558 my %inner = (a =>1, b => 2, c => 3);
1559 sub TIEHASH { bless \%inner, $_[0] }
1560 sub FIRSTKEY { $f++; my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1561 sub NEXTKEY { $n++; each %{$_[0]} }
1564 print "[$x][$f][$n]\n";
1567 print "[$x][$f][$n]\n";
1572 # keys(%tied) in scalar context with SCALAR present
1573 # XXX the behaviour of scalar(keys(%tied)) may change - it currently
1574 # doesn't make use of SCALAR() if present
1575 my ($f,$n, $s) = (0,0,0);
1576 my %inner = (a =>1, b => 2, c => 3);
1577 sub TIEHASH { bless \%inner, $_[0] }
1578 sub FIRSTKEY { $f++; my $a = scalar keys %{$_[0]}; each %{$_[0]} }
1579 sub NEXTKEY { $n++; each %{$_[0]} }
1580 sub SCALAR { $s++; scalar %{$_[0]} }
1583 print "[$x][$f][$n][$s]\n";
1586 print "[$x][$f][$n][$s]\n";
1591 # dying while doing a SAVEt_DELETE dureing scope exit leaked a copy of the
1592 # key. Give ASan something to play with
1593 sub TIEHASH { bless({}, $_[0]) }
1596 sub DESTROY { print "destroy\n"; }
1610 # ditto for SAVEt_DELETE with an array
1611 sub TIEARRAY { bless({}, $_[0]) }
1614 sub DESTROY { print "destroy\n"; }
1628 # This is not intended as a test of *correctness*. The precise ordering of all
1629 # the events here is observable by code on CPAN, so potentially some of it will
1630 # inadvertently be relying on it (and likely not in any regression test)
1631 # Hence this "test" here is intended as a way to alert us if any core code
1632 # change has the side effect of alerting this observable behaviour, so that we
1633 # can document it in the perldelta.
1636 my ($class, $note) = @_;
1637 bless \$note, $class;
1642 print "Destroying $$self\n";
1649 bless \$zero, shift;
1659 Note->new(++$$self);
1663 # Iteration on tied hashes is implemented by storing a copy of the last reported
1664 # key within the hash, passing it to NEXTKEY, and then freeing it (in order to
1665 # store the SV for the newly returned key)
1667 # Here FIRSTKEY/NEXTKEY return keys that are references to objects...
1675 printf "FIRSTKEY is %s %s\n", ref $k, $$k;
1677 # each calls iternext_flags, hence this is where the previous key is freed
1680 printf "NEXTKEY is %s %s\n", ref $k, $$k;
1682 # Our reference to the object is gone, but a reference remains within %h, so
1683 # DESTROY isn't triggered.
1685 print "Before untie\n";
1687 print "After untie\n";
1689 # Currently if tied hash iteration is incomplete at the untie, the SV recording
1690 # the last returned key is only freed if regular hash iteration is attempted.
1692 print "Before regular iteration\n";
1694 print "After regular iteration\n";
1704 Before regular iteration
1705 After regular iteration