#!/bin/perl -w ############################################################# # # # This example performs the same transformation as ex_fm3 # # It uses TwigRoots and TwigPrintOutsideRoots to be really # # efficient. # # # # This example updates the dates in freshmeat.xml # # freshmeat dates look like Feb 17th 2000, 21:03 or # # February 17th 2000, 21:03 # # # # This tool converts them to ISO (yyyy-mm-dd) format # # # # Dates are processed and output, while the rest of the # # XML document is simply output as fast as possible # # # ############################################################# use strict; use XML::Twig; # just a my %mm= ( Jan => "01", Feb => "02", Mar => "03", Apr => "04", May => "05", Jun => "06", Jul => "07", Aug => "08", Sep => "09", Oct => "10", Nov => "11", Dec => "12"); my $twig= new XML::Twig( # create the twig TwigRoots => { created => \&upd_date, # the 2 elements will be processed updated => \&upd_date # the same way }, TwigPrintOutsideRoots => 1, # just spit out the rest unchanged ); if( my $file= $ARGV[0]) { $twig->parsefile( $file); } # process the twig else { $twig->parse( \*STDIN); } exit; sub upd_date { my( $twig, $date)= @_; my $fancy_date= $date->text; # get the date if ($fancy_date=~ /\A(\w\w\w)\w* (\d+)\w\w (\d\d\d\d)/) { my( $month, $day, $year)= ($mm{$1}, $2, $3); $date->set_text( "$year-$month-$day"); # convert it } else { die "can't parse date $fancy_date"; } $date->print; # print the date }