Part one in a three part series of articles on Perl an XML has been posted over at IBM DeveloperWorks:

"This series is a guide to those who need a quick XML-and-Perl solution. In a surprisingly large number of cases, you only need one tool to integrate XML into a Perl application, XML::Simple. Part 1 tells you where to get it, how to use it, and where to go next. Once you whet your appetite for working with XML in Perl, the other two articles in this series will help you sharpen your new skills further."

Enjoy the Slashdot flame war discussion.

Martin

Replies are listed 'Best First'.
Re: XML plus Perl -- simply magic
by dragonchild (Archbishop) on Feb 04, 2007 at 04:09 UTC
    It's funny. There are 73 articles on Perl.com about Perl and XML. Not a single blip on Slashdot. The moment IBM realizes that Perl can handle XML, the world sits up and takes notice. Maybe it's because IBM realizes Perl is useful vs. Perl knowing how to handle XML?

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      I have no idea if Slashdot has linked to the Perl.com XML articles in the past, however IIRC Slashdot content is provided by user submissions. So I guess I am partly to blame, since I have never submitted any of the Perl.com XML articles :)

      I agree it is funny, that nobody else has done so in the past.

      Cheers

      Martin
        *sighs* That's the wrong blasting. XML and YAML don't solve the same problem. XML solves the problem of how to encode arbitrary data + metadata into a text format. YAML solves the problem of how to encode arbitrarily-nested data structures. AFAIK, YAML doesn't address the issue of metadata.

        Now, YAML should be used in many places that XML is used. But, if you had to choose only one to use for every single arbitrary data (+metadata) need, that solution would be XML. I'd love to be proven wrong re: YAML's inability to handle arbitrary metadata, but I don't think I am.


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: XML plus Perl -- simply magic
by Cody Pendant (Prior) on Feb 06, 2007 at 05:55 UTC
    The author of that article says that you need XML::Simple and Perl to handle his relatively simple XML task(s), because "XSLT can't do arithmetic". There's no hint that he's joking. Of course XSLT can do arithmetic! It's a very strange thing to say.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: XML plus Perl -- simply magic
by Jenda (Abbot) on Feb 15, 2007 at 18:23 UTC

    (Regarding the article (also posted to the author with a little different wording).)
    The code for the pet shop could be further shortened by using ForceArray => [dog, cat, hippo]. Not only would this make the structure returned by XMLin simpler, but it'd save the developer from having to unwrap the one item arrays. And would save some memory.

    In case anyone is interested this is the implementation of the task (except for the ordering) using XML::Rules:

    #!/usr/bin/perl -w use strict; use XML::Rules; use Date::Calc qw(Add_Delta_YM Decode_Date_EU Delta_Days Delta_YMD); u +se Data::Dumper; my @now = (localtime(time))[5, 4, 3]; $now[0] += 1900; # Perl years start in 1900 $now[1]++; # months are zero-based my $parser = XML::Rules->new( rules => [ price => sub { price => fixPrice( $_[1]->{_content}, 0.20 )}, dob => sub {age => dob2age( $_[1]->{_content})}, 'name,owner' => 'content', 'dog,cat,hippo' => sub {delete($_[1]->{_content}); $_[0] => $_[1]}, + ], style => 'filter', ); $parser->filterfile('pets.xml', \*STDOUT); # ... and the functions defined in the article
    It's 67 lines and unlike the XML::Simple implementation works as a filter ... only the data of a single animal are in memory at any time.

    With the ordering it's

    #!/usr/bin/perl -w use strict; use XML::Rules; use Date::Calc qw(Add_Delta_YM Decode_Date_EU Delta_Days Delta_YMD); u +se Data::Dumper; my @now = (localtime(time))[5, 4, 3]; $now[0] += 1900; # Perl years start in 1900 $now[1]++; # months are zero-based my $parser = XML::Rules->new( rules => [ price => sub { price => fixPrice( $_[1]->{_content}, 0.20 )}, dob => sub {age => dob2age( $_[1]->{_content})}, 'name,owner' => 'content', '_default' => 'no content array', 'pets' => sub { my ($tag, $attrs) = @_; delete($attrs->{_content}); foreach my $kind (values %{$attrs}) { next unless ref($kind) eq 'ARRAY'; # just in case $kind = [sort {$a->{name} cmp $b->{name}} @$kind]; # sort the animals of a kind by name }; return $tag => $attrs; }, ], style => 'filter', ident => ' ', ); $parser->filterfile('pets.xml', \*STDOUT); # ... and the functions (78 lines in total)
    though this of course can't work as a filter since it has to be able to sort everything. Whether that's clearer than the XML::Simple based code is a question :-)

    P.S.: You need XML::Rules 0.17 for the identation to work.