[tpm] Good examples?

Brandon McCaig bamccaig at gmail.com
Fri Nov 8 07:24:09 PST 2013

On Fri, Nov 8, 2013 at 9:30 AM,  <arocker at vex.net> wrote:
> Does anyone know of a good explanation/example of how to use XML::SAX
> anywhere online?
> I've looked at the CPAN documentation, and other usual sources, but they
> have some ambiguity about what are standard method names, features, &c.,
> and what are simply examples of user-written resources.
> The problem that started this train of thought is absurdly simple;
> extracting 3 fields per record from a STDIN stream of records containing 5
> or 6 embedded in XML tags.
>  A simple program, basically 3 regexes and a print, does the job, but
> attracted a storm of online criticism about parsing XML with regexes.
> I've been trying to write the equivalent using XML::SAX, but seem to be
> missing something. (For one thing, all the examples assume reading from a
> named file, not STDIN.)

I have used XML::Simple with some success in the past. If you just
need a proper XML parser for best practices, but don't need something
robust, then it's probably an ideal option. Of course, taking the time
to learn the larger, feature-rich modules will translate to larger
projects later on, but XML::Simple will probably get you to a solution
faster. IIRC, you basically just load the XML file/text and are given
a data structure in return that represents it. It can be a little bit
weird about how it decides what is an array ref versus a scalar.
Depending on your document you might find that awkward to work with.
The only workaround that I know of (and it's been a couple of years so
I don't remember the setting) is to force it to always create array
refs, which means when you know there's only one value you will always
have to dereference the first element, but at least the structure is
predictable that way. Good luck. :)


Brandon McCaig <bamccaig at gmail.com> <bamccaig at castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};

More information about the toronto-pm mailing list