Phoenix.pm: OOP quandry the sequel (and the answer)

Douglas E. Miles doug.miles at ns2.phoenix.bowne.com
Wed Feb 9 12:32:43 CST 2000


OK,

I discovered what my problem was.  It was simply that my packages needed
to come before the object is instantiated.  Here is a simple example:

# This works!

#!/usr/bin/perl

foreach $symname (sort(keys(%main::)))
{

  local *sym = $main::{$symname};
  print "\$$symname is defined\n" if defined $sym;
  print "\@$symname is defined\n" if defined @sym;
  print "\%$symname is defined\n" if defined %sym;
  print "\&$symname is defined\n" if defined &sym;

}

package XML_ObjTree::report;

@ISA = qw(XML_ObjTree);

package XML_ObjTree;

sub new
{

  print "NEW!!!!!!!!!!!!!\n";

}

my $obj = XML_ObjTree::report->new;


# This doesn't work.
#!/usr/bin/perl

foreach $symname (sort(keys(%main::)))
{

  local *sym = $main::{$symname};
  print "\$$symname is defined\n" if defined $sym;
  print "\@$symname is defined\n" if defined @sym;
  print "\%$symname is defined\n" if defined %sym;
  print "\&$symname is defined\n" if defined &sym;

}

my $obj = XML_ObjTree::report->new;

package XML_ObjTree::report;

@ISA = qw(XML_ObjTree);

package XML_ObjTree;

sub new
{

  print "NEW!!!!!!!!!!!!!\n";

}

Notice the difference in where this line is:

my $obj = XML_ObjTree::report->new;

I was under the mistaken assumption that this was all resolved at
compile time.  Apparently, this is not the case.

Bonus:

I also discovered something else I didn't know.  XML_ObjTree::report is
stored under the XML_ObjTree:: symbol table - not under main::.  This
suprised me because everything I have seen says that there is no implied
relationship.  I guess that just refers to the fact that there is no
automatic inheritance.  Hope this is helpful.  It was definitely
educational to me.

-- 
Socrates is a man. All men are mortal. Therefore, all mortals are
Socrates.



More information about the Phoenix-pm mailing list