Phoenix.pm: More symbol table weirdness
Douglas E. Miles
doug.miles at ns2.phoenix.bowne.com
Thu Feb 10 11:29:26 CST 2000
<snip>
> I have not tested this thing out, so all bets are off! But it may give you
> some ideas. Let me know more about what you find out...
>
> Tim
Thanks Tim. I'll give this a try and let you know. Sorry I didn't
reply to your other message. Before I got a chance to dive into what
you said, I figured out some of it my self. Just FYI, I have my program
working now. This question is still relevant though, because I'd like
to understand. Here's the code (following the code is an explanation of
what I did):
#!/usr/bin/perl
package XML_ObjTree;
# Initialize global node counter.
$Node_Counter = 0;
sub traverse
{
my $self = shift;
# Number the left side of the node.
$self->{left_node_count} = ++$Node_Counter;
local ($index);
# Loop through all kids.
foreach $index (0..$#{$self->{Kids}})
{
# Recurse kid.
$self->{Kids}->[$index]->traverse();
}
# Done with branch; number right side of the node.
$self->{right_node_count} = ++$Node_Counter;
XML_ObjTree::insert_element($self);
} # END: traverse
sub insert_element
{
my $self = shift;
# Get the element from the class name.
my $element = (reverse(split(/::/, ref($self))))[0];
my @fields = (1, $element, 1, $self->{left_node_count},
$self->{right_node_count});
my $fields = join(',', map("\'$_\'", @fields));
# SQL_Insert(sprintf($Event_Insert, $values));
} # END: insert_element
sub dump_tree
{
use Data::Dumper;
my $self = shift;
print Dumper($self);
}
#package XML_ObjTree::report;
#
#@ISA = ('XML_ObjTree');
#
#package XML_ObjTree::title;
#
#@ISA = ('XML_ObjTree');
#
#package XML_ObjTree::Characters;
#
#@ISA = ('XML_ObjTree');
package main;
use XML::Parser;
require 'xml_parser.cfg';
require 'dba.pl';
my $xml_document = shift;
my $parser = new XML::Parser(Style => 'Objects',
Pkg => 'XML_ObjTree');
my $object_tree = $parser->parsefile($xml_document);
# Grab all of the XML_ObjTree::* packages.
foreach $package (grep(/\:\:$/, keys(%XML_ObjTree::)))
{
$package =~ s/://g;
# Make XML_ObjTree::* inherit from XML_ObjTree.
my $package_declaration = <<"EOD";
package XML_ObjTree::$package;
\@ISA = ('XML_ObjTree');
EOD
eval $package_declaration;
# *base_package_stash = $XML_ObjTree::{$package};
# @{$package_stash{ISA}} = ('XML_ObjTree');
# @{$XML_ObjTree::{$package}->{ISA}} = ('XML_ObjTree');
}
$object_tree->[0]->traverse();
$object_tree->[0]->dump_tree();
############################
Basically, the new stuff is that I search through the XML_ObjTree::
package for all packages in that namespace (grep). Then I create a
package declaration, and @ISA statement, and eval them. This gives me
the generic inheritance that I was looking for. I must say that this
has been the most difficult and educational program I've done in a
while. Hope I didn't bore you guys too much. :)
--
Socrates is a man. All men are mortal. Therefore, all mortals are
Socrates.
More information about the Phoenix-pm
mailing list