object never destructs
[p5sagit/p5-mst-13.2.git] / t / op / ref.t
index b0619cb..e83a04f 100755 (executable)
@@ -1,6 +1,6 @@
 #!./perl
 
-print "1..37\n";
+print "1..50\n";
 
 # Test glob operations.
 
@@ -73,7 +73,7 @@ print ${$$ref[2]}[2] == 5 ? "ok 16\n" : "not ok 16\n";
 print scalar @{$$ref[0]} == 0 ? "ok 17\n" : "not ok 17\n";
 
 print $ref->[1] == 2 ? "ok 18\n" : "not ok 18\n";
-print $ref->[2]->[0] == 3 ? "ok 19\n" : "not ok 18\n";
+print $ref->[2]->[0] == 3 ? "ok 19\n" : "not ok 19\n";
 
 # Test references to hashes of references.
 
@@ -101,7 +101,7 @@ $subref = \&mysub;
 &$subref;
 
 $subrefref = \\&mysub2;
-&$$subrefref("ok 24\n");
+$$subrefref->("ok 24\n");
 sub mysub2 { print shift }
 
 # Test the ref operator.
@@ -134,7 +134,8 @@ print ref $object2  eq MYHASH  ? "ok 32\n" : "not ok 32\n";
 
 sub mymethod {
     local($THIS, @ARGS) = @_;
-    die "Not a MYHASH" unless ref $THIS eq MYHASH;
+    die 'Got a "' . ref($THIS). '" instead of a MYHASH'
+       unless ref $THIS eq MYHASH;
     print $THIS->{FOO} eq BAR  ? "ok $ARGS[0]\n" : "not ok $ARGS[0]\n";
 }
 
@@ -144,13 +145,14 @@ $string = "not ok 34\n";
 $object = "foo";
 $string = "ok 34\n";
 $main'anonhash2 = "foo";
-$string = "not ok 34\n";
+$string = "";
 
-sub DESTROY {
+DESTROY {
+    return unless $string;
     print $string;
 
-    # Test that the object has already been "cursed".
-    print ref shift eq HASH ? "ok 35\n" : "not ok 35\n";
+    # Test that the object has not already been "cursed".
+    print ref shift ne HASH ? "ok 35\n" : "not ok 35\n";
 }
 
 # Now test inheritance of methods.
@@ -175,5 +177,61 @@ print $foo eq foo ? "ok 37\n" : "not ok 37\n";
 sub BASEOBJ'doit {
     local $ref = shift;
     die "Not an OBJ" unless ref $ref eq OBJ;
-    $ref->{shift};
+    $ref->{shift()};
+}
+
+package UNIVERSAL;
+@ISA = 'LASTCHANCE';
+
+package LASTCHANCE;
+sub foo { print $_[1] }
+
+package WHATEVER;
+foo WHATEVER "ok 38\n";
+
+#
+# test the \(@foo) construct
+#
+package main;
+@foo = (1,2,3);
+@bar = \(@foo);
+@baz = \(1,@foo,@bar);
+print @bar == 3 ? "ok 39\n" : "not ok 39\n";
+print grep(ref($_), @bar) == 3 ? "ok 40\n" : "not ok 40\n";
+print @baz == 3 ? "ok 41\n" : "not ok 41\n";
+
+my(@fuu) = (1,2,3);
+my(@baa) = \(@fuu);
+my(@bzz) = \(1,@fuu,@baa);
+print @baa == 3 ? "ok 42\n" : "not ok 42\n";
+print grep(ref($_), @baa) == 3 ? "ok 43\n" : "not ok 43\n";
+print @bzz == 3 ? "ok 44\n" : "not ok 44\n";
+
+# test for proper destruction of lexical objects
+
+sub larry::DESTROY { print "# larry\nok 45\n"; }
+sub curly::DESTROY { print "# curly\nok 46\n"; }
+sub moe::DESTROY   { print "# moe\nok 47\n"; }
+
+{
+    my ($joe, @curly, %larry);
+    my $moe = bless \$joe, 'moe';
+    my $curly = bless \@curly, 'curly';
+    my $larry = bless \%larry, 'larry';
+    print "# leaving block\n";
+}
+
+print "# left block\n";
+
+package FINALE;
+
+{
+    $ref3 = bless ["ok 50\n"];         # package destruction
+    my $ref2 = bless ["ok 49\n"];      # lexical destruction
+    local $ref1 = bless ["ok 48\n"];   # dynamic destruction
+    1;                                 # flush any temp values on stack
+}
+
+DESTROY {
+    print $_[0][0];
 }