Hello,
it's not at all a noob level code: it's something like a grammar execution using eval
Nowadays grammars are better implemented using Marpa::R2 but is not my field.
Anyway using print YAPE::Regex::Explain and Data::Dumper all will become much clearer (I hope..):
use Data::Dumper;
use YAPE::Regex::Explain;
print "regex explanation:\n";
print "\t",YAPE::Regex::Explain->new(qr{s/\*/ && /})->explain;
print "\t",YAPE::Regex::Explain->new(qr{s/\+/ || /})->explain;
print "\t",YAPE::Regex::Explain->new(qr{s/(\w+)\s*'/!$1/g})->explain;
print "\t",YAPE::Regex::Explain->new(qr{\((?:(?>[^()]+)|(??{$re}))*\)}
+)->explain;
print "processing DATA:\n";
while(<DATA>){
chomp; # added by me
print "\n\t-->received [$_]\n";
my ($re, $term, @vars, %vars, $loops);
s/\*/ && /g;
print "\t-->after first regex [$_]\n";
s/\+/ || /g;
print "\t-->after second regex [$_]\n";
s/(\w+)\s*'/!$1/g;
print "\t-->after third regex [$_]\n";
$re = qr{\((?:(?>[^()]+)|(??{$re}))*\)}; # :-)
s/($re)\s*'/!$1/g;
print "\t-->after fourth regex [$_]\n";
$term = $_;
print "\t-->\$term is [$term]\n";
@vars = $_ =~ m/(\w+)/g;
print "\t\@vars is [@vars]\n";
$vars{$_}++ for @vars;
print "\t-->\%vars is :\n";
print "\t\t", Dumper \%vars;
@vars = sort keys %vars;
print "\t-->\@vars is [@vars]\n";
s/(\w+)/\$$1/g;
print "\t-->\$_ is [$_]\n";
printf "\n@vars = $term";
@vars = map {"\$$_"}@vars;
print "\n\t-->\@vars is [@vars]\n";
$loops .= "for $_ (0..1) {\n" for @vars;
print "\t-->\$loops is [$loops]\n";
$loops .= qq{printf "@vars = %d\n", eval;\n};
print "\t-->\$loops is [$loops]\n";
$loops .= "}" for @vars;
print "\t-->\$loops is [$loops]\n\n\n";
do{ no strict 'vars'; eval $loops };
die "Can't process $term$_\n$@\n" if $@;
}
__DATA__
a*b*!(c)
a'+b'+c'
!(a*b)
b^a
(a^b)'
(a+(a+b))'*c
which output:
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|