#!/usr/bin/perl use strict; use warnings; use XML::Parser; my $parser = new XML::Parser(Style => 'ObjectBinding', Pkg => 'MyPkg'); my ($object) = $parser->parse('hithere'); $object->my_method(); ($object) = $parser->parse(''); print "Got \"$object\"\n"; { package MyPkg::container; sub new { my ($class, $attrs, @contents) = @_; # Check contents are ok - a list of s. die "Non-thing (" . ref $_ . ") in container!" for grep ! $_->isa('MyPkg::thing'), @contents; bless { THINGS => \@contents }, $class } sub my_method { # Execute stuff on my things. my $self = shift; $_->say_hello for @{ $self->{THINGS} }; print "(", scalar( @{ $self->{THINGS} } ), " things processed.)\n"; } } { package MyPkg::thing; sub new { my ($class, $attrs, @contents) = @_; # Check contents are ok - a plain old string. @contents = ('') unless @contents; die "A should just contain text!" unless @contents == 1 and ! ref $contents[0]; bless \$contents[0], $class } sub say_hello { # What it says. my $self = shift; print "This thing contains \"$$self\".\n"; } }