D1
D2
D3
This should overwrite the old T2.
####
#!/usr/bin/perl
use strict;
use XML::Twig;
# As each tweak tag is processed, this subroutine is called. Here we will
# see if any previous tweak tag had the same name element. If so, we will
# overwrite the previous tweak tag with this new tweak tag's contents, then
# delete the new tweak tag.
sub pruner {
my $this_tweak = $_;
my $tweakname = $this_tweak->att('name');
my $exp = "/tcf/tweak[\@name=\"$tweakname\"]";
my @matches = $this_tweak->get_xpath($exp);
print "TWEAK: ";
$this_tweak->print;
print " (Found $#matches other parsed tweaks with the same name)\n";
# If the tweak's name is found elsewhere, replace the first
# instance with the latest one, then delete the latest one.
if ($#matches == 1) {
print "\tReplacing\n\t\t";
@matches[0]->print;
print "\n\twith\n\t\t";
$this_tweak->print;
print "\n";
$this_tweak->replace(@matches[0]);
}
}
my $twig = XML::Twig->new(expand_external_ents => 1,
twig_handlers => { 'tweak' => \&pruner }
);
$twig->parsefile("/home/igo/StormLogic/MythiC/Tweaker/test5.tcf");
print "TWIG:\n";
$twig->print;
print "\n";
####
$ ./twig_replace_test.pl
TWEAK: D1 (Found 0 other parsed tweaks with the same name)
TWEAK: D2 (Found 0 other parsed tweaks with the same name)
TWEAK: D3 (Found 0 other parsed tweaks with the same name)
TWEAK: This should overwrite the old T2. (Found 1 other parsed tweaks with the same name)
Replacing
D2
with
This should overwrite the old T2.
TWIG: