In this case it's probably easier to use a stack and a loop rather than recursion. Consider:
use warnings; use strict; my $sql = <<SQL; select A, B, C, (select D, E, F, (select G, H from AA where ID = Z) from BB where ID2 = X), I, J from CC; SQL my @parts = split /(\W+)/, $sql; my @formatting = (undef); for my $part (@parts) { print $part; if (lc $part eq 'select') { $formatting[-1] = 1; next; } if (lc $part eq 'from') { $formatting[-1] = undef; next; } if ($part =~ m'\)') { pop @formatting; } next if ! $formatting[-1]; if ($part =~ m'\(') { push @formatting, undef; next; } print qq{ format \\"\%s|\\"} if $part =~ /\w/; }
Prints:
select A format \"%s|\", B format \"%s|\", C format \"%s|\", (select D format \"%s|\", E format \"%s|\", F format \"%s|\", (select G format \"%s|\", H format \"%s|\" from AA where ID = Z) f +rom BB where ID2 = X), I format \"%s|\", J format \"%s|\" from CC;
In reply to Re: Recursive parse -
by GrandFather
in thread Recursive parse -
by real.aussie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |