• 22c
  • ·
  • 2 weeks ago
  • ·
  • [ - ]
As a polyglot, sometimes the existence of a certain library or interface greatly influences what language I decide to write something in.

Twig is indeed time-tested, but I don't see a good reason to use Twig unless you are already using Perl. I was kinda hoping this would give reasons to use Twig over proven XML libraries from other languages, but I suppose it makes sense given this article was published to a Perl specific website.

Nice write-up, though!

The basic reason to use XML::Twig as opposed to XSLT would be to do XML processing while still having access to all of Perl's power, DB access, file system integration etc... things that can be clunky to do in XSLT.

For what it's worth I have used XSLT too, and the basic programming patterns are surprisingly similar: an XPath selector triggers some code, the code has access to the XML tree. In both cases you have to pay some attention to when that code is being triggered. I prefer the XML::Twig way, to being able to use a regular programming language in the code, partly because I am used to it, but also because the XSLT XML syntax ends up being quite verbose, and leads to delimiter collisions.

XML::Twig also lets you work with "too big to fit in memory" XML, by working at sub-tree level in a nicer (IMHO) way than SAX. I am sure there are other ways these days, but the module dates back to last century, so at the time that was pretty unique.

I assume that other languages also have convenient XML libraries, so if you're more familiar with them and they work for you, then you should use them!

  • hilti
  • ·
  • 2 weeks ago
  • ·
  • [ - ]
Thank you for this article! I can also recommend Xidel which is my powerhouse for processing XML, JSON and HTML
I completely agree. It is amazing how powerful Xidel is, and also easy to use with command line oneliners - for most of my task there is no need to start writing a script or programming anything.

But it would be possible with the underlying library of Xidel, internettools [1]. Fun fact: it is implemented in Free Pascal, a language for which I have great respect and which is unfortunately underrated in 2024 compared to the newer kids on the block (Rust, Go, Zig, ...).

[1] https://github.com/benibela/internettools

It's 2024 and I'm here for new articles about XML and Perl.
We've still got more advent articles coming, including one very special day which is actually a live stream from Randal Schwartz (Learning Perl). He'll share his "version of the story of the Schwartzian Transform". https://lu.ma/giz69577
What a nice surprise seeing trusty Twig here. Breaks my heart that I have less and less use cases for Perl.
Hmm, always preferred to use libxml2, even in Perl.