#!/usr/bin/perl -w use strict; package main; #Undefined subroutine &main::uhdsfsd called at objdesmonks.pl line 96. #myobj end #fakeptr end #cObj2 end #cObj end #main end #$o is myObj=HASH(0x182a50c) my $o; #is an END before package definitions a programmer error? END { print "main end\n"; print "\$o is $o\n"; print Dumper($o); $o->printObj(); } package cObj; sub new { print "in cObj new\n"; bless({'data' => 'value'}); } sub DESTROY { print "cObj destroyed\n"; #system("pause"); } END { print "cObj end\n"; } package cObj2; sub new { print "in cObj2 new\n"; bless({'data2' => 'value2'}); } sub DESTROY { print "cObj2 destroyed\n"; #system("pause"); } END { print "cObj2 end\n"; } package fakeptr; sub new { print "in fakeptr new\n"; bless( do{\(my $o = 999000999)}), } sub DESTROY { print "fakeptr destroyed\n"; } END { print "fakeptr end\n"; } package myObj; use Data::Dumper; use Scalar::Util qw( weaken ); my %instances = (); sub new { my $ret; print "in myObj new\n"; $ret = bless({cobj => cObj::new(), cobj2 => "not created yet" ,fakeptr => fakeptr->new()}); $instances{($ret+0)} = $ret; weaken($instances{($ret+0)}); return $ret; } sub addChild { $_[0]->{cobj2} = cObj2::new(); } sub printObj { print "printing object\n ".Dumper($_[0]); } sub DESTROY { print "myObj destroyed\n"; print Dumper($_[0]); } END { print "myobj end\n"; #comment this out to disable instance clearing #print "myobj clearing instances\n"; #for(keys %instances) { # %{$instances{$_}} = (); #} } package main; use Data::Dumper; use Devel::Peek; #if main END is here, main END fires before non main ENDs #Undefined subroutine &main::uhdsfsd called at objdesmonks.pl line 83. #main end #$o is myObj=HASH(0x182a50c) #........................ #myobj end #fakeptr end #cObj2 end #cObj end #END { # print "main end\n"; # print "\$o is $o\n"; # print Dumper($o); # $o->printObj(); #} $o = myObj::new(); my $b = myObj::new(); sub mysub { print Dumper($o); $o->addChild(); uhdsfsd(); #system("pause"); } print $b; $b = 0; mysub(); 0; #### C:\Documents and Settings\Owner\Desktop\>perl objdesmonks.pl in myObj new in cObj new in fakeptr new in myObj new in cObj new in fakeptr new myObj=HASH(0x18f7974)myObj destroyed $VAR1 = bless( { 'cobj' => bless( { 'data' => 'value' }, 'cObj' ), 'fakeptr' => bless( do{\(my $o = 999000999)}, 'fakeptr' ), 'cobj2' => 'not created yet' }, 'myObj' ); fakeptr destroyed cObj destroyed $VAR1 = bless( { 'cobj' => bless( { 'data' => 'value' }, 'cObj' ), 'fakeptr' => bless( do{\(my $o = 999000999)}, 'fakeptr' ), 'cobj2' => 'not created yet' }, 'myObj' ); in cObj2 new Undefined subroutine &main::uhdsfsd called at objdesmonks.pl line 124. myobj end fakeptr end cObj2 end cObj end main end $o is myObj=HASH(0x182a50c) $VAR1 = bless( { 'cobj' => bless( { 'data' => 'value' }, 'cObj' ), 'fakeptr' => bless( do{\(my $o = 999000999)}, 'fakeptr' ), 'cobj2' => bless( { 'data2' => 'value2' }, 'cObj2' ) }, 'myObj' ); printing object $VAR1 = bless( { 'cobj' => bless( { 'data' => 'value' }, 'cObj' ), 'fakeptr' => bless( do{\(my $o = 999000999)}, 'fakeptr' ), 'cobj2' => bless( { 'data2' => 'value2' }, 'cObj2' ) }, 'myObj' ); cObj2 destroyed fakeptr destroyed cObj destroyed myObj destroyed $VAR1 = bless( { 'cobj' => undef, 'fakeptr' => undef, 'cobj2' => undef }, 'myObj' ); C:\Documents and Settings\Owner\Desktop\>