On a quest for a pure-perl replacement for XML::Parser I made
this post to comp.lang.perl.misc which closely follows this strategy (follow the thread for a small bug fix). The OP might want to look at this for an example of how you might go about writing such a beast.
There are better pure-perl alternatives, I know. This is just a short example of one that works.