Running Perl 5.8.8. I'm parsing files in a directory tree using File::Find and sucking each file into $str in order to manipulate it in various ways. One of those manipulations is trying to perform conditional code substitution al,la CPP. Have a file that reads:
first line #ifdef ABC second line conditional on ABC defined #else second line conditional on ABC not defined #endif third linethat's in the $str var. I have a var, lets call it $macro = 'ABC' and I'm doing
$str =~ m/^#ifdef\s+($macro)$(.*)(^#else$)?(.*)^#endif$/sm;Ok that parses and I get matches, but a number of issues with that:
1) the first (.*) swallows everything before the ^#endif - now if it wasn't a string with embedded new lines, I could use [^#] to stop before the #else, but I only want to stop before an entire #else, at the beginning of a line at the beginning of the line, a #else in the middle of the line must pass thru.
2) once I'm matching ok, I want to substitute the string between the ABC and the #else, or the string between #else and #endif, depending on if $macro = ABC.
I think I need to use assertions, but that's as far as I can figure and having pawed over PP 3rd ed I still haven't figured it out.
Sorry if this covered somewhere, if it is, please just point me to it.
Seasons Greetings
In reply to assertions help by DavidFerrington
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |