XML technologies / XML / Perl & XML / Programmer strategies / A comics directory
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
XSLT is one thing, but the possibilities of combining Perl, XML, and the Web are almost unlimited. Sometimes rearranging existing XML and producing output is not enough. The output information may need enriching, the transformation may be too complex for XSLT, or there may be other reasons. The previous example already went some way in this direction: we combined using XSLT to transform a DocBook document with generating an index page.
In RSS, SOAP and other XML applications we already discussed the synchronization of information on the Web. There, we dealt with RSS. In this section, we want to do the same for ComicsML. We will write a simple CGI script that builds a directory of the user’s favorite online comics. (In our imaginary world, all the sites concerned provide descriptive ComicsML documents.)
#!/usr/bin/perl
# Ein sehr einfaches Programm zur Bearbeitung von ComicsML-Dokumenten.
# Die Dokumente werden gelesen, die enthaltenen Strips in eine Liste eingefügt,
# und dann wird eine Webseite mit Links zu den betreffenden Comics ausgegeben.
use warnings;
use strict;
use XML::ComicsML; # ... damit wir ComicsML-Objekte bearbeiten können
use CGI qw(:standard);
use LWP;
use Date::Manip; # Ausgabe eines Datums ist sonst zu kompliziert
# Wir gehen davon aus, daß die URLs der ComicsML-Dokumente in einer
# Datei auf unserer Festplatte liegen, je eine URL pro Zeile.
# (Kein XML? Peinlich, peinlich ...)
my $url_file = $ARGV[0] or die "Usage: $0 url-file\n";
my @urls; # Liste der URLs
open (URLS, $url_file) or die "Kann die Datei $url_file nicht lesen: $!\n";
while (<URLS>) { chomp; push @urls, $_; }
close (URLS) or die "Kann die Datei $url_file nicht schließen: $!\n";
# Der LWP-UserAgent ist eine Art in Perl geschriebener Browser
my $ua = LWP::UserAgent->new;
my $parser = XML::ComicsML->new;
my @strips; # Liste der gelesenen Comic-Strips
foreach my $url (@urls) {
my $request = HTTP::Request->new(GET=>$url);
my $result = $ua->request($request);
my $comic; # Das zu lesende Comic-Objekt
if ($result->is_success) {
# Versuchen wir, die Datei mit dem ComicsML-Parser zu lesen.
unless ($comic = $parser->parse_string($result->content)) {
# Sieht nicht gut aus...
warn "Das Dokument $url ist kein korrektes XML-Dokument!\n";
next;
}
} else {
warn "Die URL $url konnte nicht gelesen werden: " . $result->status_line . "\n";
next;
}
# Als nächstes extrahieren wir alle einzelnen Strips aus dem Comic. Jeder Strip
# wird in eine Hashreferenz umgewandelt und abgelegt.
foreach my $strip ($comic->strips) {
push (@strips, {strip=>$strip, comic_title=>$comic->title, comic_url=>$comic->url});
}
}
# Strips nach Datum sortieren und ausgeben (dazu verwenden wir die Funktion UnixDate
# von Date::Manip).
my @sorted = sort {UnixDate($$b{strip}->date, "%s") <=> UnixDate($$a{strip}->date, "%s")}
@strips;
# Als nächstes bauen wir eine Webseite.
print header;
print start_html("Die aktuellsten Comics");
print h1("Links zu den aktuellsten Comics...");
# Die sortierte Liste in umgekehrter Reihenfolge: aktuelle Comics zuerst
foreach my $strip_info (@sorted) {
my ($title, $url, $svg);
my $strip = $$strip_info{strip};
$title = join (" - ", $strip->title, $strip->date);
# Link ausgeben
if ($url = $strip->url) {
$title = "<a href='$url'>$title</a>";
}
# Analog für den Titel des Comics
my $comic_title = $$strip_info{comic_title};
if ($$strip_info{comic_url}) {
$comic_title = "<a href='$$strip_info{comic_url}'>$comic_title</a>";
}
# Titelzeile ausgeben
print p("<b>$comic_title</b>: $title");
print "<hr />";
}
print end_html; Compared with the effort we put into the example Apache::DocBook, this program may seem too simple: it has no built-in cache, does not limit the number of strips processed, and makes no attempt at an attractive layout. As a quick hack, however, it is quite useful, works, and demonstrates the benefit of a helper module such as XML::ComicsML.
Our tour of the world of Perl and XML ends here. As we mentioned at the start of this book, the relationship between the two technologies is still fairly young. The full potential of their combination has yet to be realized. While we were writing this book, new parsers such as XML::LibXML and philosophies such as PerlSAX2 were only just emerging. Nevertheless, we hope the information provided is sufficient. We encourage you to become part of this world, where many fascinating developments will continue to emerge in the coming years.
<aloha>Happy hacking!</aloha>
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top