Slashdot Mirror


XML and Perl

davorg writes "One of Perl's great strengths is in processing text files. That is, after all, why it became so popular for generating dynamic web pages -- web pages are just text (albeit text that is supposed to follow particular rules). As XML is just another text format, it follows that Perl will be just as good at processing XML documents. It's therefore surprising that using Perl for XML processing hasn't received much attention until recently. That's not saying that there hasn't been work going on in that area -- many of the Perl XML processing modules have long and honourable histories -- it's just that the world outside of the Perl community doesn't seem to have taken much notice of this work. This is all set to change with the publication of this book and O'Reilly's Perl and XML." Read on to see how well Davorg thinks this book introduces XML text processing with Perl to the wider world. XML and Perl author Mark Riehl, Ilya Sterin pages 378 publisher New Rider rating 8 reviewer Davorg ISBN 0735712891 summary Good introduction to processing XML with Perl

XML and Perl is written by two well-known members of the Perl XML community. Both are frequent contributors to the "perl-xml" mailing list, so there's certainly no doubt that they know what they are talking about. Which is always a good thing in a technical book.

The book is made up of five sections. The first section has a couple of chapters which introduce you to the concepts covered in the book. Chapter one introduces you separately to XML and Perl and then chapter two takes a first look at how you can use Perl to process XML. This chapter finishes with two example programs for parsing simple XML documents.

Section two goes into a lot more detail about parsing XML documents with Perl. Chapter three looks at event-driven parsing using XML::Parser and XML::Parser::PerlSAX to demonstrate to build example programs before going to talk in some detail about XML::SAX which is currently the state of the art in event-driven XML parsing in Perl. It also looks at XML::Xerces which is a Perl interface to the Apache Software Foundation's Xerces parser. Chapter four covers tree based XML parsing and presents examples using XML::Simple, XML::Twig, XML::DOM and XML::LibXML. In both of these chapters the pros and cons of each of the modules are discussed in detail so that you can easily decide which solution to use in any given situation.

Section three covers generating XML documents. In chapter five we look at generating XML from text sources using simple print statements and also the modules XML::Writer and XML::Handler::YAWriter. Chapter six looks at taking data from a database and turning that into XML using modules like XML::Generator::DBI and XML::DBMS. Chapter seven looks at miscellaneous other input formats and contains examples using XML::SAXDriver::CSV and XML::SAXDriver::Excel.

Section four covers more advanced topics. Chapter eight is about XML transformations and filtering. This chapter covers using XSLT to transform XML documents. It covers the modules XML::LibXSLT, XML::Sabletron and XML::XPath.

Chapter nine goes into detail about Matt Sergeant's AxKit, the Apache XML Kit which allows you to create a website in XML and automatically deliver it to your visitors in the correct format.

Chapter ten rounds off the book with a look at using Perl to create web services. It looks at the two most common modules for creating web services in Perl - XML::RPC and SOAP::Lite.

Finally, section five contains the appendices which provide more background on the introductions to XML and Perl from chapter one.

There was one small point that I found a little annoying when reading the book: Each example was accompanied with a sample of the XML documents to be processed together with both a DTD and an XML Schema definition for the document. This seemed to me to be overkill. Did we really need both DTDs and XML Schemas for every example. I would have found it less distracting if one (or even both) of these had been moved to an appendix.

That small complaint aside, I found it a useful and interesting book. It will be very useful to Perl programmers (like myself) who will increasingly be expected to process (and provide) data in XML formats.

You can purchase XML and Perl from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

29 of 138 comments (clear)

  1. XML is NOT just text! by Anonymous Coward · · Score: 5, Insightful

    The whole point of XML is that it is NOT just a string of text. That's why Perl isn't particularly any better than Java or C++ or VB or whatever for processing XML - you're going to be using a library that gives you SAX or DOM access to your XML, and you'll never need to know that there's a text representation being serialized onto some wires somewhere.

    1. Re:XML is NOT just text! by DaRobin · · Score: 3, Interesting

      True and then not so. Perl's flexible data structures and OO make it a simpler approach than languages that think XML == Object Serialisation. It is also very likely that a lot of what you're going to see flying by in SAX or hanging around in DOM will be text. Sometimes lots of it, sometimes text that has non-XML structure and requires microparsing.

      But anyway, what really puts Perl ahead of the pack (together with Python, the only viable competitor I've tried -- Java is really lagging these days) is its large wealth of SAX (and to a lesser degree, DOM) tools. All sorts of very useful filters can be grabbed, complex pipeline management is a given, the SAX writing framework is cool, there are SAX parsers for many non-XML formats, etc.

      --
      Radioactive cats have 18 half-lives.
    2. Re:XML is NOT just text! by consumer · · Score: 3, Insightful
      Let's see...

      • Editable in emacs (or vi). Check.
      • Grep-able. Check.
      • Diff-able. Check.
      • Understandable to the naked eye. Check.

      Sure smells like text to me.

    3. Re:XML is NOT just text! by Anonymous Coward · · Score: 3, Insightful
      What you're looking at there is one possible representation of an XML document. What you can see is NOT XML. XML is an idea - a hierarchical data structure. If you're manipulating some XML programatically, you should be manipulating this hierarchical data structure, and you'll be using some sort of API (SAX or DOM, probably) to do so. You should emphatically NOT be manipulating text strings. Any code of the form
      tag = tag + "</" + tagname + ">"
      means you're doing it wrong.

      So, no, XML is not editable in emacs (or vi), grep-able, diff-able or understandable to the naked eye. Go and think about it again.

    4. Re:XML is NOT just text! by EvlG · · Score: 2, Insightful

      I think it is interesting to note that this is precisely the reason that XML is poorly suited for any task that requires human intervention.

    5. Re:XML is NOT just text! by cygnus · · Score: 2, Insightful
      you're doing it wrong.

      ...

      So, no, XML is not editable in emacs (or vi), grep-able, diff-able or understandable to the naked eye. Go and think about it again.

      yes it is.. just because you claim that "you're doing it wrong," doesn't mean it's impossible.

      xml is text just as much as html is.. are you going to tell me that html isn't editable in emacs or human-readable? how is html different from DocBook, for example?

      --
      Just raise the taxes on crack.
    6. Re:XML is NOT just text! by orcrist · · Score: 2, Insightful

      The whole point of XML is that it is NOT just a string of text. That's why Perl isn't particularly any better than Java or C++ or VB or whatever for processing XML - you're going to be using a library that gives you SAX or DOM access to your XML, and you'll never need to know that there's a text representation being serialized onto some wires somewhere.

      I'll respond to you though many others are making similar arguments. First of all, when you say "XML is NOT just text!" do you mean "XML is NOT merely text" or "XML is not solely text"? I'll agree with the first, but the second is generally not true.

      What noone seems to be mentioning is what you get out of those libraries: you get the entire structure in nodes thanks to the library's parser, but what are the contents of those nodes? Text! You might argue that the element names and most of the attributes are either defined by the dtd/schema, etc. but at least CDDATA will often be abitrary text. And, at least in my experience (mostly web-based applications), there will often be a need to process some of that text, e.g. extract links which are embedded in the text, convert newlines to <br>s, and many other things. And then, isn't it handy when the language reading the contents of those nodes has strong text-handling abilities?

      Just a thought.

      -chris

      --
      San Francisco values: compassion, tolerance, respect, intelligence
    7. Re:XML is NOT just text! by grantm · · Score: 2, Insightful
      What you're looking at there is one possible representation of an XML document.

      I couldn't agree less. In fact, XML is one possible representation of the abstract hierarchical data structure you described. Furthermore, XML is in fact a text representation. There are many other ways you could represent that data structure (eg: a custom binary format, records in a relational or hierarchical database, a object serialised to a binary stream etc) but none of them are XML.

      The W3C themselves say that "XML is text" and then go on to point out that advantages of being a text format include:

      • you can look at data without needing the program that produced it
      • you can read it with you favourite text editor
      • it's easier for developers to debug

      They also say: "Like HTML, XML files are text files that people shouldn't have to read, but may when the need arises".

      In parallel with the development of XML, our notion of the definition of 'text' has also moved forward. Through the adoption of standards like Unicode and bridging facilities like encoding declarations, we have moved past 7-bit ASCII as being the one true text.

      To claim that an XML file is not "editable in emacs (or vi), grep-able, diff-able or understandable to the naked eye" is demonstrably untrue. You'll obviously need a text editor that understands whichever encoding the file uses (both emacs and vim fit that bill) but a text editor is a perfectly servicable tool for viewing and editing XML (obviously not the best tool in many cases, but acceptable nontheless)

  2. Petal by Chris+Croome · · Score: 3, Informative

    One new, and cool, Perl XML module that people might not know about is Petal (PErl Template Attribute Language).

    It is an implementation of the Zope TAL (Template Attribute Language) specification and it basically allows you to create XML templates where all the templating commands are just attributes of existing tags.

    This allows things like XHTML templates which are very WYSIWYG friendly since the editors don't do anything with attributes that they don't know about.

    --
    Check out MKDoc a mod_perl CMS
  3. Re:Nice by DaRobin · · Score: 5, Informative

    Would be nice to have a book with more than just one chapter on web services.

    You might be interested in Programming Web Services with Perl then.

    --
    Radioactive cats have 18 half-lives.
  4. This was a review? by Syris · · Score: 4, Insightful
    I'm sorry, but this just wasn't a terribly deep review and well below par for /. Listing contents of a book and then nitpicking a detail don't a book review make.


    How effective were the examples? How easy to read and understand were the general concepts? Were the descriptions of libraries and API's clear? Was the writing generally readable?


    Would this book even make a good reference?


    Jeez, anyone want to follow up the post with a real review?

  5. Re:Natural? by mortonda · · Score: 3, Informative
    Not really. If you're using XML as "just another text format", then you're making a funamental mistake. Within your software, you should always be treating XML as a hierarchical data structure, not as a text stream. Apart from manipulating CDATA or attribute value text, Perl has no particular strength with XML.

    Indeed, the perl only XML libraries are quite slow. I believe most of the quality perl XML handling is done by modules that use C libraries to do the grunt work. However, if the data in the XML itself is text data, then of course, perl and XML are a good match. Add SOAP and mod_perl into the mix, and you got some very nifty tools.

  6. XML frees us from Perl by Euphonious+Coward · · Score: 4, Interesting
    The whole point of XML is to free us from having to do the kinds of things Perl is meant for. Absent free-form text munging, Perl really has no advantage over other languages. At the same time, it has real deficits for people who need to know they have solved a problem correctly and completely.

    (For reference, see this rant by the brilliant net.kook Erik Naggum. The most quotable bit, for the lazy among you, is

    ...[Perl] rewards idiotic behavior in a way that no other language or tool has ever done, and on top of it, it punishes conscientiousness and quality craftsmanship -- put simply: you can commit any dirty hack in a few minutes in perl, but you can't write an elegant, maintainabale program that becomes an asset to both you and your employer; you can make something work, but you can't really figure out its complete set of failure modes and conditions of failure. (how do you tell when a regexp has a false positive match?)
    )

    1. Re:XML frees us from Perl by DaRobin · · Score: 2, Informative


      Not much help? If you start counting the number of Perl modules that expose a SAX interface to non-XML data (not to mention the host of other super-useful SAX tools) you'll probably find only one egal, Python.




      And if you think that XML has freed us from additional text processing, you obviously haven't used XML much, or at least without much variety. Most people seem constantly bent on including microlanguages in attribute values or text content. Those need good text processing.


      --
      Radioactive cats have 18 half-lives.
    2. Re:XML frees us from Perl by glwtta · · Score: 4, Insightful
      how do you tell when a regexp has a false positive match?

      A what? You (or rather the brilliant person being quoted) either mean that it matches a string that the expression isn't supposed to, which would be a serious bug in the language (and I am not aware of any such bugs); or you mean that it matches correctly, but matches things you didn't expect it to, in which case you tell, by (gasp!) testing your code. In any case, how do you tell a "false positive" regexp match in Java?

      but you can't write an elegant, maintainabale program that becomes an asset to both you and your employer

      Perhaps you can't. I have, and I do.

      --
      sic transit gloria mundi
    3. Re:XML frees us from Perl by scrytch · · Score: 2, Insightful

      Maybe the author was unable to write anything but hacks, and couldn't make anything elegant or maintainable. I've written programs with multiple subsystems, and put them well into maintenance without a lick of trouble, all in perl.

      Yes, $dd->updsp( 1,3, @ad ) looks worse than $Driver->update_displays( $Display:LOBBY, $Display:CUSTSERV, @additional ), and boy it's just a shame that perl doesn't let me use meaningful identifiers or document API's or forward declare functions for arg checking ahead of time. Oh wait... Really. The argument is dead, continuing to raise it is just trolling.

      I switched to python because I got tired of leaning on my shift key. Tcl has probably the prettiest syntax for me, but as a language it's braindead beyond belief (not to mention slow)

      --
      I've finally had it: until slashdot gets article moderation, I am not coming back.
  7. The Right Tool for the Right Job by nathanz · · Score: 2, Funny

    I think one of the main reasons Perl and XML aren't generally used together is because Perl isn't object oriented in the same way the Java and C# are. I know that OO concepts have been bolted on to Perl in the same way the OO was bolted on to C++ and in my opinion with similar results (i.e., kludge-fest). It's very natual in Java to parse an XML doc and get an object, while it's more natural to parse a log file or CSV file with Perl.

  8. Perl is a reflection of your soul by Nexus7 · · Score: 4, Interesting

    Well, perhaps not your soul, but your Perll code just reflects the way you think to a greater extent than other languages. This isn't something that's done underhandedly, it is well advertised in every posting in c.l.perl and the Camel book, and every other book about Perl. Which is that Perl is not at all orthogonal, TMTOWDI (there's more than one way to do it). If you want to be rigorous and declare everything and not have your typos become references automatically, you "use strict" and your magic line is "#!/usr/bin/perl -w". If not, well Perl allows you to do that too. If you want objects, you can do that, if not, not.

    If is possible to write quality code in Perl Just because the language allows you to not do so isn't its fault. It doesn't stop you from doing it, because that'd stop you from doing brilliant things.

    To address some specific things you mentioned, you can do full-fledged exception handling in Perl if you want to (with eval and specific modules), or, you know, not. And I'm not familiar with the false positive matches in regexps (perhaps you're referring to some famous problem). But if a regexp doesn't do what you want it to, isn't is wrong? Between // and tr and split I get along just fine.

  9. Re:Formalised features of Perl (in this book?) by Mr.+Droopy+Drawers · · Score: 2, Interesting

    As you're also aware, most Comp Sci courses fawn over Pascal, a VERY formalized language. However, it's not mentioned much past education circles (and Apple afficionados).

    In practice, reference counting doesn't seem to lead to memory leaks as you describe. And, I would argue it is much more efficient than Java's method.

    PERL is an excellent SCRIPTING language. Larry Wall describes it as a "glue" language. XML is a good thing to glue together. It's perfect for that. Every tool has its purpose; push any too far, and you start abusing it.

    Trying to find the quote from Larry Wall. I think it goes something like this: "Perl did easy things easily and made impossible things doable."

    --

    To Copy from One is Plagiarism; To Copy from Many is Research.

  10. Re:You lost me on the incredible leap of logic... by sheriff_p · · Score: 3, Insightful

    Ah no, see, you forgot to read the first line:

    "One of Perl's great strengths is in processing text files."

    Perl is good at handling text files. XML is a text file. Therefore, Perl is good at handling XML.

    As opposed to:

    My pasta maker is good at making pasta. Pasta is a type of food. Ice-cream is also food. Therefore, my pasta maker is good at making ice-cream.

    Does that help?

    --
    Score:-1, Funny
  11. So, where's the review? by mattdm · · Score: 3, Insightful

    I see the table of contents explained in paragraph form. And then one complaint about the organization of the book. And then I expect to read the review, but it's already on to "you can buy this book here", and user comments.

    I know complaining about slashdot stories is like shooting those proverbial barreled fish, but sheesh.

  12. Re:If you REALLY want to buy the book by graxrmelg · · Score: 3, Informative

    But if people are interested in getting a good price rather than putting a commission into your pocket (and contributing to a company that abuses software patents), maybe they should order it from Bookpool instead, for $3 less than Amazon. (I don't have any affiliation with Bookpool.)

  13. Re:If you REALLY want to buy the book by Badger · · Score: 2, Informative

    Actually, /. used to link to Amazon, and had an affiliate program. Once Amazon started enforcing their one-click patent, and the Amazon boycott began, /. switched to Fatbrain (which was bought by BN).

  14. XML::Simple by Anonymous Coward · · Score: 2, Interesting

    I'm seeing a lot of comments that perl doesn't have any particular strengths when dealing with XML. A good module people should check out is XML::Simple. Basically, it automagically turns XML into a nested data structure, and automagically turns a nested data structure into XML. The great thing about it you just make a single API call, and just directly access the data from there without having to learn anything more complicated. Definitely not an end-all solution, but definitely handles the common case wonderfully, and has quite a few handy options to allow more fine tuned control.

  15. Re:You lost me on the incredible leap of logic... by IpalindromeI · · Score: 3, Insightful

    Except that your syllogism is faulty, whereas his is not.

    His:
    1. (from earlier in his post) Perl is well suited for processing all text formats.
    2. XML is a text format.
    3. Therefore, Perl is well suited for processing XML.

    Yours:
    1. Your pasta maker is good at making pasta.
    2. Pasta is a type of food.
    3. Therefore, your pasta maker is good at making all types of food (for example, ice cream).

    You can see that he went from general to specific, whereas you went from specific to general. He argues that being able to do all things in a given set (process all text formats) gives the ability to do one of the things in that set (process a particular text format). You argue that being able to do one thing in a set (make a particular food) gives the ability to do all things in the set (make all foods).

    You could save your argument by changing your middle point to be "All foods are a type of pasta," and then your conclusion becomes trivially true. But you'd also have to get everyone to agree that ice cream is pasta.

    --

    --
    Promoting critical thinking since 1994.
  16. hasn't received much attention until recently? by HealYourChurchWebSit · · Score: 4, Informative



    The reviewer is correct, Perl is a good tool for slamming and jammin' text, including XML. What I'm not so sure of is the quote "It's therefore surprising that using Perl for XML processing hasn't received much attention until recently."

    I mean one need only scroll down the extensive list of CPAN Modules to see well over 50, as well as many sites/authors devoting time, energy and resource.

    Similarly, I would point out some press modules supporting web services via XML, such as SOAP::Lite as far back as 02/26/01 and XML-RPC also in '01 -- or O'Reilly's own XML.com with articles such as "Processing XML with Perl" written shortly after the turn of the millenium.

    Point is, though I personally love Perl, blatant plugs such as "... it's just that the world outside of the Perl community doesn't seem to have taken much notice of this work. This is all set to change with the publication of this book and O'Reilly's Perl and XML." " don't inspire confidence in the reviewer's objectivity.

    --
    --- have you healed your church website?
  17. Axkit, perl & XML so happy together by porter235 · · Score: 2, Informative

    check it out. http://axkit.org/

    "Apache AxKit is an XML Application Server for Apache. It provides on-the-fly conversion from XML to any format, such as HTML, WAP or text using either W3C standard techniques, or flexible custom code. AxKit also uses a built-in Perl interpreter to provide some amazingly powerful techniques for XML transformation."

    picture coccoon for perl. using perl for xsp pages and doing pipline transformations on xml. great stuff.

  18. Re:i hate perl... by etcshadow · · Score: 2, Insightful

    "I once rewrote a Perl parser in Java and went from 9hrs to 45mins"

    Well, shit. I once rewrote a Perl parser in *Perl* and went from 9hrs to 45mins. What the hell kind of flame-bait shit is this!?

    It is true that extremely well-written C code can outperform perl code at anything. It is also true that for things that perl is made for (like ripping through tons of text-data), a typical Perl program will *most likely* do it better than a typical C program, simply because it is making use of more optimized underlying algorithms (even though the actual execution structure is slightly more bloated than C... double-dereferencing pointers, compile-time imediately before run-time, etc). ... However, Java is just as goddamn interpretted as Perl, if not more so! Perl compiles to *native* byte-code prior to execution, unless you are talking about eval'd strings, whereas Java sits in non-native byte-code that has to be interpretted real-time by the VM. Best case: you have a good just-in-time compiler that pulls Java up to even with Perl (that is, compiled imediately prior to run-time into native byte-code).

    Also, Java has all the same disavantages with respect to C... that is more insulation from the *actual* memory (no such thing as a real pointer in either, garbage-collection, etc).

    Anyway, bottom-line is this. If what you say is at all true, then you had a shittily-written Perl program. I promise you that I can write just as shitty a program in Java... does that mean that we should trash Java?!?!? Abso-f*cking-lutely not! I'll do you one better, too: I'll write just as shitty and slow of a parser in Java that doesn't even *look* that bad to someone who doesn't understand the subtleties behind such simple abstractions as strings, lists and arrays.

    I'm very serious with what I said originaly, I have, in fact, taken a Perl parser (a super-light-weight XML parser, actually) and reduced the parse-time by several orders of magnitude. The idiot who wrote it originaly (myself), went walking through the string or stream looking for 's (with a regexp), at the highest level. It is *terribly* slow to strip leading characters off of a long string in Perl (I'm pretty sure that it copies the whole goddamn string, minus those 10 (or however many) characters on the front). I made a *very* simple change, namely this:

    # split on positive lookahead assertion of a ''
    # then we just deal individually with blocks of text that all start
    # with a ''... should save time
    my @xml = split(/(?=)/,' '.$xml);
    shift @xml;

    And, you'll note that I f*cking commented it (something which people just don't seem to understand when they trash perl). Bang! Many orders of magnitude in speed improvement. Simple.

    Anyway, pull your head out of your ass.

    --
    :Wq
    Not an editor command: Wq
  19. Re:You lost me on the incredible leap of logic... by Golias · · Score: 2, Insightful
    As XML is just another text format, it follows that Perl will be just as good at processing XML documents.

    Since my pasta maker is good at making pasta, and ice cream and pasta are both foods, it follows my pasta maker will be just as good at making ice cream.

    That only correlates if ice cream is a type of pasta, because XML is a text format.

    This is a lot more like saying "since my pasta maker is good at making Ziti, Rigate, Macaroni, etc., all pastas really, and Spaghetti is a type of pasta, my pasta maker should be good at making Spaghetti.

    --

    Information wants to be anthropomorphized.