Logicus has asked for the wisdom of the Perl Monks concerning the following question:
Ok here is a very rough and ugly start to how I think your suggesting I should go about compiling aXML files. I know this code is _AWFUL_, but it does work and it's quite fast at turning an aXML file into a bunch of print statements and axml function calls which could be very quickly parsed by my existing parser system as they are very short. I guess I would want to save the output of this program on first run so I can skip this step on subsequent runs. This only supports <> type tags.
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 <qd><qd><qd>a</qd></qd></qd> = 42 <conf>bar</conf> <sometag>thatdoesnothing</sometag> but also needs to be present in the output <conf>foo</conf> <someothertag>thatalsodoesnothing</someothertag> 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, $k +ey); } 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_stri +ng@s) { $compiled_string_middle = "<axml>$1$2$3</axml>"; } #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</axml>\n\n"; $replacement .= 'print qq@'; $replacement .= $3; $replacement .= "@;\n\n<axml>$4"; $mong_string .= $replacement; } $compiled_string_middle =~ s@`@@gs; if ($compiled_string_middle =~ m@.*$command_opens_string(.*?)$comma +nd_closes_string</axml>$@s) { $mong_string .= "$2$3</axml>\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>(.*?)<\/axml>/print axml\(qq\@$1\@\);/g; return $mong_string; } my $sortofcompiled_string = sortofcompileit($aXML_ENV,$plugins,$aXML_s +tring); say $sortofcompiled_string; my $end = [ gettimeofday ]; my $total_elapsed = tv_interval($start,$end); say "elapsed = $total_elapsed";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: rough start of an axml compiler
by Boldra (Deacon) on Aug 01, 2011 at 12:02 UTC | |
by Logicus (Initiate) on Aug 01, 2011 at 19:29 UTC | |
by Logicus (Initiate) on Aug 02, 2011 at 12:40 UTC | |
by Boldra (Deacon) on Aug 02, 2011 at 13:06 UTC | |
by Logicus (Initiate) on Aug 02, 2011 at 15:03 UTC | |
by Logicus (Initiate) on Aug 02, 2011 at 18:13 UTC | |
by Anonymous Monk on Aug 02, 2011 at 02:34 UTC | |
by Boldra (Deacon) on Aug 02, 2011 at 13:24 UTC | |
by Anonymous Monk on Aug 02, 2011 at 13:46 UTC | |
Re: rough start of an axml compiler
by pemungkah (Priest) on Jul 21, 2011 at 16:48 UTC | |
by Logicus (Initiate) on Jul 21, 2011 at 19:53 UTC | |
by pemungkah (Priest) on Jul 22, 2011 at 06:35 UTC | |
by Logicus (Initiate) on Jul 22, 2011 at 06:42 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom