#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $xmlParser = XML::Twig->new(); # Fetch the new books to add to the catalog my $new_items = fetch_xml('new_items.xml'); my @add; for my $book ($new_items->root->children('book')) { push @add, $book->copy(); } print scalar(@add), " new books to add to the catalog!\n"; # Add the books to the catalog my $cur_catalog = fetch_xml('current_catalog.xml'); for my $book (@add) { # Each book will be added after the last book in the catalog $book->paste(last_child => $cur_catalog->root); } # Now dump our current catalog $cur_catalog->flush( \*STDERR, pretty_print=>'indented' ); sub fetch_xml { my $fname = shift; my $txt = do { open my $FH, '<', $fname or die "$fname: $!"; local $/; <$FH>; }; $xmlParser->parse($txt); return $xmlParser; }