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
31 # standard behaviour, without any extra references
33 {package Tie::HashUntie;
34 use base 'Tie::StdHash';
40 tie %h, Tie::HashUntie;
46 # standard behaviour, with 1 extra reference
48 $a = tie %h, Tie::StdHash;
53 # standard behaviour, with 1 extra reference via tied
61 # standard behaviour, with 1 extra reference which is destroyed
63 $a = tie %h, Tie::StdHash;
69 # standard behaviour, with 1 extra reference via tied which is destroyed
78 # strict behaviour, without any extra references
86 # strict behaviour, with 1 extra references generating an error
89 $a = tie %h, Tie::StdHash;
92 untie attempted while 1 inner references still exist at - line 6.
95 # strict behaviour, with 1 extra references via tied generating an error
102 untie attempted while 1 inner references still exist at - line 7.
105 # strict behaviour, with 1 extra references which are destroyed
106 use warnings 'untie';
108 $a = tie %h, Tie::StdHash;
114 # strict behaviour, with extra 1 references via tied which are destroyed
115 use warnings 'untie';
117 tie %h, Tie::StdHash;
124 # strict error behaviour, with 2 extra references
125 use warnings 'untie';
127 $a = tie %h, Tie::StdHash;
131 untie attempted while 2 inner references still exist at - line 7.
134 # strict behaviour, check scope of strictness.
137 $A = tie %H, Tie::StdHash;
140 use warnings 'untie';
142 tie %h, Tie::StdHash;
149 # Forbidden aggregate self-ties
150 sub Self::TIEHASH { bless $_[1], $_[0] }
156 Self-ties of arrays and hashes are not supported at - line 6.
159 # Allowed scalar self-ties
161 sub Self::TIESCALAR { bless $_[1], $_[0] }
162 sub Self::DESTROY { $destroyed = 1; }
167 die "self-tied scalar not DESTROYed" unless $destroyed == 1;
171 # Allowed glob self-ties
174 sub Self2::TIEHANDLE { bless $_[1], $_[0] }
175 sub Self2::DESTROY { $destroyed = 1; }
176 sub Self2::PRINT { $printed = 1; }
180 tie *$c, 'Self2', $c;
183 die "self-tied glob not PRINTed" unless $printed == 1;
184 die "self-tied glob not DESTROYed" unless $destroyed == 1;
188 # Allowed IO self-ties
190 sub Self3::TIEHANDLE { bless $_[1], $_[0] }
191 sub Self3::DESTROY { $destroyed = 1; }
192 sub Self3::PRINT { $printed = 1; }
194 use Symbol 'geniosym';
196 tie *$c, 'Self3', $c;
199 die "self-tied IO not PRINTed" unless $printed == 1;
200 die "self-tied IO not DESTROYed" unless $destroyed == 1;
204 # TODO IO "self-tie" via TEMP glob
206 sub Self3::TIEHANDLE { bless $_[1], $_[0] }
207 sub Self3::DESTROY { $destroyed = 1; }
208 sub Self3::PRINT { $printed = 1; }
210 use Symbol 'geniosym';
212 tie *$c, 'Self3', \*$c;
215 die "IO tied to TEMP glob not PRINTed" unless $printed == 1;
216 die "IO tied to TEMP glob not DESTROYed" unless $destroyed == 1;
220 # Interaction of tie and vec
224 tie $a,Tie::StdScalar or die;
233 # correct unlocalisation of tied hashes (patch #16431)
235 tie %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'};
242 # An attempt at lvalueable barewords broke this
245 Can't modify constant item in tie at - line 3, near "'main';"
246 Execution of - aborted due to compilation errors.
249 # localizing tied hash slices
252 print exists $ENV{FooA} ? 1 : 0, "\n";
253 print exists $ENV{FooB} ? 2 : 0, "\n";
254 print exists $ENV{FooC} ? 3 : 0, "\n";
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";
261 print exists $ENV{FooA} ? 7 : 0, "\n";
262 print exists $ENV{FooB} ? 8 : 0, "\n";
263 print exists $ENV{FooC} ? 9 : 0, "\n"; # this should not exist
276 # FETCH freeing tie'd SV
277 sub TIESCALAR { bless [] }
278 sub FETCH { *a = \1; 1 }
284 # [20020716.007] - nested FETCHES
286 sub F1::TIEARRAY { bless [], 'F1' }
291 sub F2::TIEARRAY { bless [2], 'F2' }
292 sub F2::FETCH { my $self = shift; my $x = $f1[3]; $self }
296 print $f2[4][0],"\n";
298 sub F3::TIEHASH { bless [], 'F3' }
303 sub F4::TIEHASH { bless [3], 'F4' }
304 sub F4::FETCH { my $self = shift; my $x = $f3{3}; $self }
308 print $f4{'foo'}[0],"\n";
314 # test untie() from within FETCH
316 sub TIESCALAR { my $pkg = shift; return bless [@_], $pkg; }
319 my ($obj, $field) = @$self;
320 untie $obj->{$field};
321 $obj->{$field} = "Bar";
324 tie $a->{foo}, "Foo", $a, "foo";
325 my $s = $a->{foo}; # access once
326 # the hash element should not be tied anymore
327 print defined tied $a->{foo} ? "not ok" : "ok";
331 # the tmps returned by FETCH should appear to be SCALAR
332 # (even though they are now implemented using PVLVs.)
334 sub TIEHASH { bless {} }
335 sub TIEARRAY { bless {} }
342 my $s = "$r1 ". ref($r1) . " $r2 " . ref($r2);
346 SCALAR SCALAR SCALAR SCALAR
348 # [perl #23287] segfault in untie
349 sub TIESCALAR { bless $_[1], $_[0] }
351 tie $var, 'main', \$var;
355 # Test case from perlmonks by runrig
356 # http://www.perlmonks.org/index.pl?node_id=273490
357 # "Here is what I tried. I think its similar to what you've tried
358 # above. Its odd but convenient that after untie'ing you are left with
359 # a variable that has the same value as was last returned from
360 # FETCH. (At least on my perl v5.6.1). So you don't need to pass a
361 # reference to the variable in order to set it after the untie (here it
362 # is accessed through a closure)."
367 my ($class,$code) = @_;
377 tie $var, 'MyTied', sub { untie $var; 4 };
393 # [perl #22297] cannot untie scalar from within tied FETCH
397 tie $x, 'Overlay', $ref, $x;
403 #print "WILL EXTERNAL UNTIE $ref\n";
409 #print "counter = $counter\n";
411 print (($counter == 1) ? "ok\n" : "not ok\n");
418 my ($ref, $val) = @_;
419 return bless [ $ref, $val ], $pkg;
425 my ($ref, $val) = @$self;
426 #print "WILL INTERNAL UNITE $ref\n";
435 # [perl #948] cannot meaningfully tie $,
436 package TieDollarComma;
440 return bless \my $x, $pkg;
446 print "STORE set '$$self'\n";
456 tie $,, 'TieDollarComma';
458 print "join", "things", "up\n";
461 join<FETCH>BOBBINSthings<FETCH>BOBBINSup
473 $_[0]->{$_[1]} = $_[2];
486 return 0 if ! keys %{$_[0]};
487 sprintf "%i/%i", scalar keys %{$_[0]}, scalar keys %{$_[0]};
491 tie my %h => "TieScalar";
494 print scalar %h, "\n"
495 if %h; # this should also call SCALAR but implicitly
497 print scalar %h, "\n"
498 if !%h; # this should also call SCALAR but implicitly
508 # test scalar on tied hash when no SCALAR method has been given
516 $_[0]->{$_[1]} = $_[2];
525 my $a = keys %{ $_[0] };
531 tie my %h => "TieScalar";
540 print "not empty\n" if %h;
541 print "not empty\n" if %h;
543 my ($k,$v) = each %h;
545 print "not empty\n" if %h;
547 print "empty\n" if ! %h;
562 sub TIESCALAR { bless {} }
563 sub FETCH { my $x = 3.3; 1 if 0+$x; $x }
569 sub TIESCALAR { bless {} }
570 sub FETCH { shift()->{i} ++ }
576 # Bug 53482 (and maybe others)
577 sub TIESCALAR { my $foo = $_[1]; bless \$foo, $_[0] }
578 sub FETCH { ${$_[0]} }
579 tie my $x1, "main", 2;
580 tie my $y1, "main", 8;
583 tie my $x2, "main", "2";
584 tie my $y2, "main", "8";
591 sub TIEHASH { bless {}, $_[0] }
592 sub STORE { $_[0]->{$_[1]} = $_[2] }
593 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
594 sub NEXTKEY { each %{$_[0]} }
595 sub DELETE { delete $_[0]->{$_[1]} }
596 sub CLEAR { %{$_[0]} = () }
599 print scalar keys %h, "\n";
604 print scalar keys %h, "\n";
610 sub foo::TIESCALAR { bless {value => $_[1]}, $_[0] }
611 sub foo::FETCH { $_[0]->{value} }
612 tie my $VAR, 'foo', '42';
613 foreach my $var ($VAR) {
614 print +($var eq $VAR) ? "yes\n" : "no\n";
619 sub TIEARRAY { bless [], 'main' }
624 print "tied\n" if tied @a;
627 sub TIEHASH { bless [], 'main' }
632 print "tied\n" if tied %h;
635 # RT 20727: PL_defoutgv is left as a tied element
636 sub TIESCALAR { return bless {}, 'main' }
641 select(); # this used to coredump or assert fail
647 # RT 23810: eval in die in FETCH can corrupt context stack
649 my $file = 'rt23810.pm';
655 my ($str, $eval) = @_;
656 open my $fh, '>', $file or die "Can't create $file: $!\n";
661 eval { require $pm; $s .= '-ENDE' }
671 sub TIEHASH { bless {} }
674 # 10 or more syntax errors makes yyparse croak()
675 my $bad = q{$x+;$x+;$x+;$x+;$x+;$x+;$x+;$x+;$x+$x+;$x+;$x+;$x+;$x+;;$x+;};
677 if ($_[1] eq 'eval') {
679 eval q[BEGIN { die; $s .= '-X1' }];
681 eval q[BEGIN { $x+ }];
685 $s .= '-S1' while $@ =~ /syntax error at/g;
688 $s .= '-S2' while $@ =~ /syntax error at/g;
690 elsif ($_[1] eq 'require') {
693 q[BEGIN { die; $s .= '-X1' }],
698 for my $i (0..$#text) {
700 do_require($txt[$i], 0) if $e;;
701 do_require($txt[$i], 1);
704 elsif ($_[1] eq 'exit') {
705 eval q[exit(0); print "overshot eval\n"];
708 print "unknown key: '$_[1]'\n";
715 for my $action(qw(eval require)) {
716 $s = ''; $e = 0; $s .= main->FETCH($action); print "$action: s0=$s\n";
717 $s = ''; $e = 1; eval { $s .= main->FETCH($action)}; print "$action: s1=$s\n";
718 $s = ''; $e = 0; $s .= $foo{$action}; print "$action: s2=$s\n";
719 $s = ''; $e = 1; eval { $s .= $foo{$action}}; print "$action: s3=$s\n";
721 1 while unlink $file;
724 print "overshot main\n"; # shouldn't reach here
727 eval: s0=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
728 eval: s1=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
729 eval: s2=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
730 eval: s3=EVAL-BD-BS-E1-S1-E2-S2-S2-S2-S2-S2-S2-S2-S2-S2-S2-R
731 require: s0=REQUIRE-0-ERQ-ENDRQ-1-ERQ-ENDRQ-2-ERQ-ENDRQ-3-ERQ-ENDRQ-R
732 require: s1=REQUIRE-0-RQ
733 require: s2=REQUIRE-0-ERQ-ENDRQ-1-ERQ-ENDRQ-2-ERQ-ENDRQ-3-ERQ-ENDRQ-R
734 require: s3=REQUIRE-0-RQ
736 # RT 8857: STORE incorrectly invoked for local($_) on aliased tied array
739 sub TIEARRAY { bless [], $_[0] }
740 sub TIEHASH { bless [], $_[0] }
741 sub FETCH { $_[0]->[$_[1]] }
742 sub STORE { $_[0]->[$_[1]] = $_[2] }
751 foreach ($a[0], $h{a}) {
754 # on failure, chucks up 'premature free' etc messages
758 # the initial fix for this bug caused tied scalar FETCH to be called
759 # multiple times when that scalar was an element in an array. Check it
760 # only gets called once now.
762 sub TIESCALAR { bless [], $_[0] }
764 sub FETCH { $c++; 0 }
766 sub STORE { $c += 100; 0 }
774 my $x = $a[0] + $h{foo} + $a[$i] + (@a)[0];
779 # Bug 68192 - numeric ops not calling mg_get when tied scalar holds a ref
780 sub TIESCALAR { bless {}, __PACKAGE__ };
783 print "fetching... "; # make sure FETCH is called once per op
787 tie $foo, __PACKAGE__;
790 print "+ ", 0 + $foo, "\n";
791 print "** ", $foo**1, "\n";
792 print "* ", $foo*1, "\n";
793 print "/ ", $foo*1, "\n";
794 print "% ", $foo%123457, "\n";
795 print "- ", $foo-0, "\n";
796 print "neg ", - -$foo, "\n";
797 print "int ", int $foo, "\n";
798 print "abs ", abs $foo, "\n";
799 print "== ", 123456 == $foo, "\n";
800 print "< ", 123455 < $foo, "\n";
801 print "> ", 123457 > $foo, "\n";
802 print "<= ", 123456 <= $foo, "\n";
803 print ">= ", 123456 >= $foo, "\n";
804 print "!= ", 0 != $foo, "\n";
805 print "<=> ", 123457 <=> $foo, "\n";
808 fetching... ** 123456
813 fetching... neg 123456
814 fetching... int 123456
815 fetching... abs 123456
824 # Ties returning overloaded objects
828 '*{}' => sub { print '*{}'; \*100 },
829 '@{}' => sub { print '@{}'; \@100 },
830 '%{}' => sub { print '%{}'; \%100 },
831 '${}' => sub { print '${}'; \$100 },
834 $_ => sub { print "$op"; 100 }
835 } qw< 0+ "" + ** * / % - neg int abs == < > <= >= != <=> <> >
837 $o = bless [], overloaded;
839 sub TIESCALAR { bless {}, "" }
840 sub FETCH { print "fetching... "; $o }
844 $ghew=undef; 1+$ghew; print "\n";
845 $ghew=undef; $ghew**1; 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; print "\n";
851 $ghew=undef; int $ghew; print "\n";
852 $ghew=undef; abs $ghew; print "\n";
853 $ghew=undef; 1 == $ghew; 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 >=1; print "\n";
858 $ghew=undef; $ghew != 1; print "\n";
859 $ghew=undef; $ghew<=>1; print "\n";
860 $ghew=undef; <$ghew>; print "\n";
861 $ghew=\*shrext; *$ghew; print "\n";
862 $ghew=\@spled; @$ghew; print "\n";
863 $ghew=\%frit; %$ghew; print "\n";
864 $ghew=\$drile; $$ghew; print "\n";
888 # RT 51636: segmentation fault with array ties
892 print "ok\n"; # if we got here we didn't crash
896 sub TIEARRAY { bless {} }
897 sub STORE { tie my @b, 'T' }
904 # RT 8438: Tied scalars don't call FETCH when subref is dereferenced
906 sub TIESCALAR { bless {} }
910 sub FETCH { $fetch++; sub { $called++ } }
914 print "fetch=$fetch\ncalled=$called\n";
920 # tie mustn't attempt to call methods on bareword filehandles.
921 sub IO::File::TIEARRAY {
922 die "Did not want to invoke IO::File::TIEARRAY";
924 fileno FOO; tie @a, "FOO"
926 Can't locate object method "TIEARRAY" via package "FOO" at - line 5.
929 # STORE freeing tie'd AV
930 sub TIEARRAY { bless [] }
931 sub STORE { *a = []; 1 }
939 # CLEAR freeing tie'd AV
940 sub TIEARRAY { bless [] }
941 sub CLEAR { *a = []; 1 }
950 # FETCHSIZE freeing tie'd AV
951 sub TIEARRAY { bless [] }
952 sub FETCHSIZE { *a = []; 100 }
962 # [perl #86328] Crash when freeing tie magic that can increment the refcnt
964 eval { require Scalar::Util } or print("ok\n"), exit;
969 *TIEARRAY = *TIEHASH;
978 Scalar::Util::weaken($o->[0] = $a);
983 Scalar::Util::weaken($p->[0] = $b);
986 # Done setting up the evil data structures
996 # Localising a tied COW scalar should not make it read-only.
998 sub TIESCALAR { bless [] }
999 sub FETCH { __PACKAGE__ }
1012 # Nor should it be impossible to tie COW scalars that are already PVMGs.
1014 sub TIESCALAR { bless [] }
1016 undef $x; # downgrade to PVMG
1017 $x = __PACKAGE__; # PVMG + COW
1020 print STDERR "ok\n";
1022 # However, one should not be able to tie read-only glob copies, which look
1023 # a bit like kine internally (FAKE + READONLY).
1025 Internals::SvREADONLY($y,1);
1030 Modification of a read-only value attempted at - line 16.
1033 # Similarly, read-only regexps cannot be tied.
1034 sub TIESCALAR { bless [] }
1036 Internals::SvREADONLY($y,1);
1040 Modification of a read-only value attempted at - line 6.
1043 # tied() should still work on tied scalars after glob assignment
1044 sub TIESCALAR {bless[]}
1046 sub f::TIEHANDLE{bless[],f}
1049 [$rin]; # call FETCH
1050 print ref tied $rin, "\n";
1051 print ref tied *$rin, "\n";
1057 # (un)tie $glob_copy vs (un)tie *$glob_copy
1058 sub TIESCALAR { print "TIESCALAR\n"; bless [] }
1059 sub TIEHANDLE{ print "TIEHANDLE\n"; bless [] }
1060 sub FETCH { print "never called\n" }
1065 print "ok 1\n" if !tied $f;
1066 () = $f; # should not call FETCH
1068 print "ok 2\n" if !tied *foo;
1076 # RT #8611 mustn't goto outside the magic stack
1077 sub TIESCALAR { warn "tiescalar\n"; bless [] }
1078 sub FETCH { warn "fetch()\n"; goto FOO; }
1080 warn "before fetch\n";
1082 warn "before FOO\n";
1089 Can't find label FOO at - line 4.
1092 # RT #8611 mustn't goto outside the magic stack
1093 sub TIEHANDLE { warn "tiehandle\n"; bless [] }
1094 sub PRINT { warn "print()\n"; goto FOO; }
1096 warn "before print\n";
1098 warn "before FOO\n";
1105 Can't find label FOO at - line 4.
1108 # \&$tied with $tied holding a reference before the fetch (but not after)
1110 sub TIESCALAR {bless[]}
1121 # \&$tied with $tied holding a PVLV glob before the fetch (but not after)
1123 sub TIEARRAY {bless[]}
1129 print &{\&$$elem}, "\n";
1134 # \&$tied with $tied holding a PVGV glob before the fetch (but not after)
1135 local *72 = sub { 73 };
1136 sub TIESCALAR {bless[]}
1141 print &{\&$x}, "\n";
1146 # Lexicals should not be visible to magic methods on scope exit
1147 BEGIN { unless (defined &DynaLoader::boot_DynaLoader) {
1148 print "HASH\nHASH\nARRAY\nARRAY\n"; exit;
1150 use Scalar::Util 'weaken';
1152 sub TIEHASH { Scalar::Util::weaken($_[1]); bless \$_[1], xoufghd:: }
1153 *TIEARRAY = *TIEHASH;
1155 bless ${$_[0]} || return, 0;
1158 # hashes: ties before backrefs
1162 tie %hash, xoufghd::, \%hash;
1165 # hashes: backrefs before ties
1169 weaken(my $x = \%hash);
1170 tie %hash, xoufghd::, \%hash;
1173 # arrays: ties before backrefs
1177 tie @array, xoufghd::, \@array;
1180 # arrays: backrefs before ties
1184 weaken(my $x = \@array);
1185 tie @array, xoufghd::, \@array;
1200 # Localising a tied variable with a typeglob in it should copy magic
1201 sub TIESCALAR{bless[]}
1202 sub FETCH{warn "fetching\n"; *foo}
1216 # tied returns same value as tie
1217 sub TIESCALAR{bless[]}
1218 $tyre = \tie $tied, "";
1219 print "ok\n" if \tied $tied == $tyre;
1224 # tied arrays should always be AvREAL
1226 sub TIEARRAY{bless[]}
1229 \@_; # used to produce: av_reify called on tied array at - line 7.
1234 # [perl #67490] scalar-tying elements of magic hashes
1235 sub TIESCALAR{bless[]}
1246 # [perl #35865, #43011] autovivification should call FETCH after STORE
1247 # because perl does not know that the FETCH would have returned the same
1248 # thing that was just stored.
1250 # This package never likes to take ownership of other people’s refs. It
1251 # always makes its own copies. (For simplicity, it only accepts hashes.)
1253 sub TIEHASH { bless {} }
1254 sub FETCH { $_[0]{$_[1]} }
1255 sub STORE { $_[0]{$_[1]} = { %{ $_[2] } } }
1257 tie my %h, copier::;
1259 print $h{i}{j}, "\n";
1264 # [perl #8931] FETCH for tied $" called an odd number of times.
1267 sub A::TIESCALAR {bless [] => 'A'}
1268 sub A::FETCH {print ++ $i, "\n"}
1269 my @a = ("", "", "");
1282 # [perl #9391] return value from 'tied' not discarded soon enough
1289 sub T::TIEARRAY { my $s; bless \$s => "T" }