DavidFerrington has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: assertions help
by ikegami (Patriarch) on Dec 22, 2009 at 18:33 UTC | |
|
Re: assertions help
by BrowserUk (Patriarch) on Dec 22, 2009 at 19:07 UTC | |
by DavidFerrington (Acolyte) on Dec 22, 2009 at 19:48 UTC | |
by BrowserUk (Patriarch) on Dec 22, 2009 at 20:19 UTC | |
|
Re: assertions help
by kennethk (Abbot) on Dec 22, 2009 at 18:34 UTC | |
|
Re: assertions help
by AnomalousMonk (Archbishop) on Dec 22, 2009 at 20:55 UTC | |
by DavidFerrington (Acolyte) on Dec 23, 2009 at 00:04 UTC | |
by AnomalousMonk (Archbishop) on Dec 23, 2009 at 03:20 UTC | |
|
Re: assertions help
by DavidFerrington (Acolyte) on Dec 22, 2009 at 23:50 UTC |