use Modern::Perl;
use Time::HiRes qw( gettimeofday tv_interval );
my $start = [ gettimeofday ];
my $aXML_ENV = { qd => { a => 'b',
b => 'c',
c => '42' },
conf => { x => 'y',
foo => '1',
bar => '2' }
};
my $plugins = { qd => '$result = $aXML_ENV->{"qd"}->{$data}',
conf => '$result = $aXML_ENV->{"conf"}->{$data}' };
my $aXML_string = qq@
some other data that needs to be output as well
a = 42
bar
thatdoesnothing but also needs
to be present in the output
foo
thatalsodoesnothing
as far as aXML is concerned, but obviously used by
whatever program we are sending this data too.
@;
sub sortofcompileit
{
my $aXML_ENV = $_[0];
my $plugins = $_[1];
my $aXML_string = $_[2];
my $compiled_string_start;
my $compiled_string_end;
my $compiled_string_middle;
my @commands;
my $command_opens_string = "(";
my $command_closes_string = "(";
my $mong_string;
while ( my ($key, $value) = each(%$plugins) ) { push (@commands, $key); }
map { $command_opens_string .= "<$_>|" } @commands;
map { $command_closes_string .= "$_>|" } @commands;
chop $command_opens_string;
chop $command_closes_string;
$command_opens_string .= ")";
$command_closes_string .= ")";
#find the position of the first command
#set everything before it to be printed
if ($aXML_string =~ m@^(.*?)$command_opens_string@s)
{
$compiled_string_start = 'print qq@';
$compiled_string_start .= $1;
$compiled_string_start .= "@;\n\n";
}
$mong_string = 'use aXML;';
$mong_string .= "\n";
$mong_string .= $compiled_string_start;
#find everything in the middle
if ($aXML_string =~ m@$command_opens_string(.*)$command_closes_string@s)
{
$compiled_string_middle = "$1$2$3";
}
#find anything in the middle which is inbetween any type of close
#and open and set it to be printed out
my $replacement;
$compiled_string_middle =~ s@$command_closes_string@`$1@gs;
while ($compiled_string_middle =~ m@(.*?)$command_closes_string([^`]*?)$command_opens_string@gs)
{
$replacement = "$1$2\n\n";
$replacement .= 'print qq@';
$replacement .= $3;
$replacement .= "@;\n\n$4";
$mong_string .= $replacement;
}
$compiled_string_middle =~ s@`@@gs;
if ($compiled_string_middle =~ m@.*$command_opens_string(.*?)$command_closes_string$@s)
{
$mong_string .= "$2$3\n\n";
}
#find the position of the last close tag
#set everything after it to be printed
if ($aXML_string =~ m@.*$command_closes_string(.*)$@s)
{
$compiled_string_end = 'print qq@';
$compiled_string_end .= $2;
$compiled_string_end .= '@;';
}
$mong_string .= $compiled_string_end;
$mong_string =~ s@`@@gs;
$mong_string =~ s/(.*?)<\/axml>/print axml\(qq\@$1\@\);/g;
return $mong_string;
}
my $sortofcompiled_string = sortofcompileit($aXML_ENV,$plugins,$aXML_string);
say $sortofcompiled_string;
my $end = [ gettimeofday ];
my $total_elapsed = tv_interval($start,$end);
say "elapsed = $total_elapsed";