my $name= join '/', split /::/, $Names{$pkg}; #### #!/usr/bin/perl -w use strict; use XML::Parser::Lite; my $doc = "\n"; print "'$doc'\n"; # you could of course build the file name from the package name here # instead of just having it hard-coded my %Names = ( foo => { file => 'Foo/Bar.pm', package => 'Foo::Bar'}, ); my @stack; my $parser = XML::Parser::Lite->new( Handlers => { Start => sub { shift; print "Start: @_\n"; my $pkg = shift; eval { require $Names{$pkg}->{file}; }; if ($@) { die "Cannot require ' $Names{$pkg}->{file}' ($pkg)\n"; } my $x = $Names{$pkg}->{package}->new(@_); push @stack, $x; }, End => sub { shift; print "End: @_\n"; pop @stack if @stack; }, }, ); $parser->parse($doc); print "Finally done\n";