#!/usr/bin/perl
use strict;
use warnings;
use XML::Parser;
use Data::Dumper;
my $text = 'hithere';
my $parser = new XML::Parser(Style => 'Objects', Pkg => 'MyPkg');
my $object = $parser->parse($text)->[0];
print Dumper($object);
{ package MyPkg::container;
# use base 'XML::Element'; # Or something like that?
# Constructor method, nothing exciting happening here so
# it's redundant but just to show I could have one...
sub start_tag {
my ($class, $container) = shift;
# The parser should use start_tag's return value when
# returning this parsed object to anything containing it (or
# the caller if this is the root object).
bless {}, $class
}
sub end_tag { # Should get contained objects as values
# Check accumulated contents and store values in my hash -
# here I'm expecting only a list of s...
my ($self, @contents) = @_;
die "Non-thing (" . ref $_ . ") in container!" for
grep ! $_->isa('MyPkg::thing'), @contents;
$self->{THINGS} = \@contents;
}
sub myMethod { # Execute stuff using parsed objects
}
}
{ package MyPkg::thing;
# use base 'XML::Element'; # Or something like that?
sub end_tag { # Should get contained objects as values
# Check accumulated contents and store values in my hash -
# here I'm expecting only plain text in the form of scalars.
my ($self, @contents) = @_;
die "Non-scalar (" . ref $_ . ") in thing!" for
grep ref $_, @contents;
$self->{DATA} = join('', @contents);
}
}