in reply to Best Macro Syntax for Perl?

A couple random observations, thrown out sort of at random.

I've implemented macro substitution code in Perl; the real core of it was this, stolen shamelessly from Mastering Regular Expresssions:

# Define base variable detector: any (possibly-nested) angle-bracketed + string. # Patterns to extract <variables> or >variables< from a string. my $out_angled; $out_angled = qr/ < ( [^<>] | (??{$out_angled}) )* > /x; # open angle-bracket then ... # non-angle chars ... # or ... # another angle-bracketed item ... # if there are any .. +. # and a close angle +-bracket
This detects arbitrarily-nested angle-bracket expressions. You would want to, I think, switch the macro brackets to a different string - angle-brackets were a requirement in my application. Fortunately the strings I was substituting in didn't need angle brackets for anything else.

If you want to look at the complete class that implements macro expansion, take a look at App::SimpleScan::Substitution, which is the core of it. This routine as supplied is probaly horrendous overkill, because it was concerned with doing combinatorial substitutions (i,e, var1 has 3 values, var2 has 2, and var3 has 7, so a string containing all 3 variables results in 3*2*7 = 42 different expansions). You might consider gutting it to only do single substitutions, or simply only allow your variable dictionaries to have one value per variable.

The code is pretty complex, because it allows for expansions that create strings that need to be re-expanded, because the values are actually further macro expansions. This lets you define really complex relationships "down the tree", and simply substitute one variable in at the top to expand all the possibilities.

Like I said, probably total overkill for your application, but maybe the variable substitution and detection stuff will help. Probably the most difficult part of the code was remembering what had already been substituted so that recurrences of the variable (from expansion of other constructs) were substituted with the same value.

The pattern match at the top will help a lot in discovering the macro variables. You should be able to rewire it for multi-character delimiters like this:

$templaty = qr/ \[% # open delim ( (?!%\]) # not-a-closer lookahead . # anything else )* # as many as there are %\] # close delim /x; @vars = ($string =~ $templaty);
Take a look at pages 261 to 281 of Mastering Regular Expressions. Those 20 pages were worth the entire purchase cost of the book.