1N/A#!./perl
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = qw(. ../lib);
1N/A}
1N/A
1N/Aprint "1..69\n";
1N/A
1N/Arequire 'test.pl';
1N/A
1N/A# Test glob operations.
1N/A
1N/A$bar = "ok 1\n";
1N/A$foo = "ok 2\n";
1N/A{
1N/A local(*foo) = *bar;
1N/A print $foo;
1N/A}
1N/Aprint $foo;
1N/A
1N/A$baz = "ok 3\n";
1N/A$foo = "ok 4\n";
1N/A{
1N/A local(*foo) = 'baz';
1N/A print $foo;
1N/A}
1N/Aprint $foo;
1N/A
1N/A$foo = "ok 6\n";
1N/A{
1N/A local(*foo);
1N/A print $foo;
1N/A $foo = "ok 5\n";
1N/A print $foo;
1N/A}
1N/Aprint $foo;
1N/A
1N/A# Test fake references.
1N/A
1N/A$baz = "ok 7\n";
1N/A$bar = 'baz';
1N/A$foo = 'bar';
1N/Aprint $$$foo;
1N/A
1N/A# Test real references.
1N/A
1N/A$FOO = \$BAR;
1N/A$BAR = \$BAZ;
1N/A$BAZ = "ok 8\n";
1N/Aprint $$$FOO;
1N/A
1N/A# Test references to real arrays.
1N/A
1N/A@ary = (9,10,11,12);
1N/A$ref[0] = \@a;
1N/A$ref[1] = \@b;
1N/A$ref[2] = \@c;
1N/A$ref[3] = \@d;
1N/Afor $i (3,1,2,0) {
1N/A push(@{$ref[$i]}, "ok $ary[$i]\n");
1N/A}
1N/Aprint @a;
1N/Aprint ${$ref[1]}[0];
1N/Aprint @{$ref[2]}[0];
1N/Aprint @{'d'};
1N/A
1N/A# Test references to references.
1N/A
1N/A$refref = \\$x;
1N/A$x = "ok 13\n";
1N/Aprint $$$refref;
1N/A
1N/A# Test nested anonymous lists.
1N/A
1N/A$ref = [[],2,[3,4,5,]];
1N/Aprint scalar @$ref == 3 ? "ok 14\n" : "not ok 14\n";
1N/Aprint $$ref[1] == 2 ? "ok 15\n" : "not ok 15\n";
1N/Aprint ${$$ref[2]}[2] == 5 ? "ok 16\n" : "not ok 16\n";
1N/Aprint scalar @{$$ref[0]} == 0 ? "ok 17\n" : "not ok 17\n";
1N/A
1N/Aprint $ref->[1] == 2 ? "ok 18\n" : "not ok 18\n";
1N/Aprint $ref->[2]->[0] == 3 ? "ok 19\n" : "not ok 19\n";
1N/A
1N/A# Test references to hashes of references.
1N/A
1N/A$refref = \%whatever;
1N/A$refref->{"key"} = $ref;
1N/Aprint $refref->{"key"}->[2]->[0] == 3 ? "ok 20\n" : "not ok 20\n";
1N/A
1N/A# Test to see if anonymous subarrays spring into existence.
1N/A
1N/A$spring[5]->[0] = 123;
1N/A$spring[5]->[1] = 456;
1N/Apush(@{$spring[5]}, 789);
1N/Aprint join(':',@{$spring[5]}) eq "123:456:789" ? "ok 21\n" : "not ok 21\n";
1N/A
1N/A# Test to see if anonymous subhashes spring into existence.
1N/A
1N/A@{$spring2{"foo"}} = (1,2,3);
1N/A$spring2{"foo"}->[3] = 4;
1N/Aprint join(':',@{$spring2{"foo"}}) eq "1:2:3:4" ? "ok 22\n" : "not ok 22\n";
1N/A
1N/A# Test references to subroutines.
1N/A
1N/Asub mysub { print "ok 23\n" }
1N/A$subref = \&mysub;
1N/A&$subref;
1N/A
1N/A$subrefref = \\&mysub2;
1N/A$$subrefref->("ok 24\n");
1N/Asub mysub2 { print shift }
1N/A
1N/A# Test the ref operator.
1N/A
1N/Aprint ref $subref eq CODE ? "ok 25\n" : "not ok 25\n";
1N/Aprint ref $ref eq ARRAY ? "ok 26\n" : "not ok 26\n";
1N/Aprint ref $refref eq HASH ? "ok 27\n" : "not ok 27\n";
1N/A
1N/A# Test anonymous hash syntax.
1N/A
1N/A$anonhash = {};
1N/Aprint ref $anonhash eq HASH ? "ok 28\n" : "not ok 28\n";
1N/A$anonhash2 = {FOO => BAR, ABC => XYZ,};
1N/Aprint join('', sort values %$anonhash2) eq BARXYZ ? "ok 29\n" : "not ok 29\n";
1N/A
1N/A# Test bless operator.
1N/A
1N/Apackage MYHASH;
1N/A
1N/A$object = bless $main'anonhash2;
1N/Aprint ref $object eq MYHASH ? "ok 30\n" : "not ok 30\n";
1N/Aprint $object->{ABC} eq XYZ ? "ok 31\n" : "not ok 31\n";
1N/A
1N/A$object2 = bless {};
1N/Aprint ref $object2 eq MYHASH ? "ok 32\n" : "not ok 32\n";
1N/A
1N/A# Test ordinary call on object method.
1N/A
1N/A&mymethod($object,33);
1N/A
1N/Asub mymethod {
1N/A local($THIS, @ARGS) = @_;
1N/A die 'Got a "' . ref($THIS). '" instead of a MYHASH'
1N/A unless ref $THIS eq MYHASH;
1N/A print $THIS->{FOO} eq BAR ? "ok $ARGS[0]\n" : "not ok $ARGS[0]\n";
1N/A}
1N/A
1N/A# Test automatic destructor call.
1N/A
1N/A$string = "not ok 34\n";
1N/A$object = "foo";
1N/A$string = "ok 34\n";
1N/A$main'anonhash2 = "foo";
1N/A$string = "";
1N/A
1N/ADESTROY {
1N/A return unless $string;
1N/A print $string;
1N/A
1N/A # Test that the object has not already been "cursed".
1N/A print ref shift ne HASH ? "ok 35\n" : "not ok 35\n";
1N/A}
1N/A
1N/A# Now test inheritance of methods.
1N/A
1N/Apackage OBJ;
1N/A
1N/A@ISA = (BASEOBJ);
1N/A
1N/A$main'object = bless {FOO => foo, BAR => bar};
1N/A
1N/Apackage main;
1N/A
1N/A# Test arrow-style method invocation.
1N/A
1N/Aprint $object->doit("BAR") eq bar ? "ok 36\n" : "not ok 36\n";
1N/A
1N/A# Test indirect-object-style method invocation.
1N/A
1N/A$foo = doit $object "FOO";
1N/Aprint $foo eq foo ? "ok 37\n" : "not ok 37\n";
1N/A
1N/Asub BASEOBJ'doit {
1N/A local $ref = shift;
1N/A die "Not an OBJ" unless ref $ref eq OBJ;
1N/A $ref->{shift()};
1N/A}
1N/A
1N/Apackage UNIVERSAL;
1N/A@ISA = 'LASTCHANCE';
1N/A
1N/Apackage LASTCHANCE;
1N/Asub foo { print $_[1] }
1N/A
1N/Apackage WHATEVER;
1N/Afoo WHATEVER "ok 38\n";
1N/A
1N/A#
1N/A# test the \(@foo) construct
1N/A#
1N/Apackage main;
1N/A@foo = \(1..3);
1N/A@bar = \(@foo);
1N/A@baz = \(1,@foo,@bar);
1N/Aprint @bar == 3 ? "ok 39\n" : "not ok 39\n";
1N/Aprint grep(ref($_), @bar) == 3 ? "ok 40\n" : "not ok 40\n";
1N/Aprint @baz == 3 ? "ok 41\n" : "not ok 41\n";
1N/A
1N/Amy(@fuu) = \(1..2,3);
1N/Amy(@baa) = \(@fuu);
1N/Amy(@bzz) = \(1,@fuu,@baa);
1N/Aprint @baa == 3 ? "ok 42\n" : "not ok 42\n";
1N/Aprint grep(ref($_), @baa) == 3 ? "ok 43\n" : "not ok 43\n";
1N/Aprint @bzz == 3 ? "ok 44\n" : "not ok 44\n";
1N/A
1N/A# also, it can't be an lvalue
1N/Aeval '\\($x, $y) = (1, 2);';
1N/Aprint $@ =~ /Can\'t modify.*ref.*in.*assignment/ ? "ok 45\n" : "not ok 45\n";
1N/A
1N/A# test for proper destruction of lexical objects
1N/A
1N/Asub larry::DESTROY { print "# larry\nok 46\n"; }
1N/Asub curly::DESTROY { print "# curly\nok 47\n"; }
1N/Asub moe::DESTROY { print "# moe\nok 48\n"; }
1N/A
1N/A{
1N/A my ($joe, @curly, %larry);
1N/A my $moe = bless \$joe, 'moe';
1N/A my $curly = bless \@curly, 'curly';
1N/A my $larry = bless \%larry, 'larry';
1N/A print "# leaving block\n";
1N/A}
1N/A
1N/Aprint "# left block\n";
1N/A
1N/A# another glob test
1N/A
1N/A$foo = "not ok 49";
1N/A{ local(*bar) = "foo" }
1N/A$bar = "ok 49";
1N/Alocal(*bar) = *bar;
1N/Aprint "$bar\n";
1N/A
1N/A$var = "ok 50";
1N/A$_ = \$var;
1N/Aprint $$_,"\n";
1N/A
1N/A# test if reblessing during destruction results in more destruction
1N/A
1N/A{
1N/A package A;
1N/A sub new { bless {}, shift }
1N/A DESTROY { print "# destroying 'A'\nok 52\n" }
1N/A package _B;
1N/A sub new { bless {}, shift }
1N/A DESTROY { print "# destroying '_B'\nok 51\n"; bless shift, 'A' }
1N/A package main;
1N/A my $b = _B->new;
1N/A}
1N/A
1N/A# test if $_[0] is properly protected in DESTROY()
1N/A
1N/A{
1N/A my $i = 0;
1N/A local $SIG{'__DIE__'} = sub {
1N/A my $m = shift;
1N/A if ($i++ > 4) {
1N/A print "# infinite recursion, bailing\nnot ok 53\n";
1N/A exit 1;
1N/A }
1N/A print "# $m";
1N/A if ($m =~ /^Modification of a read-only/) { print "ok 53\n" }
1N/A };
1N/A package C;
1N/A sub new { bless {}, shift }
1N/A DESTROY { $_[0] = 'foo' }
1N/A {
1N/A print "# should generate an error...\n";
1N/A my $c = C->new;
1N/A }
1N/A print "# good, didn't recurse\n";
1N/A}
1N/A
1N/A# test if refgen behaves with autoviv magic
1N/A
1N/A{
1N/A my @a;
1N/A $a[1] = "ok 54\n";
1N/A print ${\$_} for @a;
1N/A}
1N/A
1N/A# This test is the reason for postponed destruction in sv_unref
1N/A$a = [1,2,3];
1N/A$a = $a->[1];
1N/Aprint "not " unless $a == 2;
1N/Aprint "ok 55\n";
1N/A
1N/A# This test used to coredump. The BEGIN block is important as it causes the
1N/A# op that created the constant reference to be freed. Hence the only
1N/A# reference to the constant string "pass" is in $a. The hack that made
1N/A# sure $a = $a->[1] would work didn't work with references to constants.
1N/A
1N/Amy $test = 56;
1N/A
1N/Aforeach my $lexical ('', 'my $a; ') {
1N/A my $expect = "pass\n";
1N/A my $result = runperl (switches => ['-wl'], stderr => 1,
1N/A prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
1N/A
1N/A if ($? == 0 and $result eq $expect) {
1N/A print "ok $test\n";
1N/A } else {
1N/A print "not ok $test # \$? = $?\n";
1N/A print "# expected ", _qq ($expect), ", got ", _qq ($result), "\n";
1N/A }
1N/A $test++;
1N/A}
1N/A
1N/Asub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
1N/A{ my $a1 = bless [3],"x";
1N/A my $a2 = bless [2],"x";
1N/A { my $a3 = bless [1],"x";
1N/A my $a4 = bless [0],"x";
1N/A 567;
1N/A }
1N/A}
1N/A$test+=4;
1N/A
1N/Amy $result = runperl (switches=>['-l'],
1N/A prog=> 'print 1; print qq-*$\*-;print 1;');
1N/Amy $expect = "1\n*\n*\n1\n";
1N/Aif ($result eq $expect) {
1N/A print "ok $test\n";
1N/A} else {
1N/A print "not ok $test\n";
1N/A foreach ($expect, $result) {
1N/A s/\n/\\n/gs;
1N/A }
1N/A print "# expected \"$expect\", got \"$result\"\n";
1N/A}
1N/A
1N/A# bug #21347
1N/A
1N/Arunperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
1N/Aif ($? != 0) { print "not " };
1N/Aprint "ok ",++$test," - UNIVERSAL::AUTOLOAD called when freeing qr//\n";
1N/A
1N/Arunperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
1N/Aif ($? != 0) { print "not " };
1N/Aprint "ok ",++$test," - warn called inside UNIVERSAL::DESTROY\n";
1N/A
1N/A
1N/A# bug #22719
1N/A
1N/Arunperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
1N/Aif ($? != 0) { print "not " };
1N/Aprint "ok ",++$test," - coredump on typeglob = (SvRV && !SvROK)\n";
1N/A
1N/A# bug #27268: freeing self-referential typeglobs could trigger
1N/A# "Attempt to free unreferenced scalar" warnings
1N/A
1N/A$result = runperl(
1N/A prog => 'use Symbol;my $x=bless \gensym,"t"; print;*$$x=$x',
1N/A stderr => 1
1N/A);
1N/Aprint "not " if length $result;
1N/Aprint "ok ",++$test," - freeing self-referential typeglob\n";
1N/Aprint "# got: $result\n" if length $result;
1N/A
1N/A# test global destruction
1N/A
1N/A++$test;
1N/Amy $test1 = $test + 1;
1N/Amy $test2 = $test + 2;
1N/A
1N/Apackage FINALE;
1N/A
1N/A{
1N/A $ref3 = bless ["ok $test2\n"]; # package destruction
1N/A my $ref2 = bless ["ok $test1\n"]; # lexical destruction
1N/A local $ref1 = bless ["ok $test\n"]; # dynamic destruction
1N/A 1; # flush any temp values on stack
1N/A}
1N/A
1N/ADESTROY {
1N/A print $_[0][0];
1N/A}