in reply to assertions help

For the simple case:

#! perl -slw use strict; my $str = q[ first line #ifdef ABC second line conditional on ABC defined #else second line conditional on ABC not defined #endif #ifdef PQR second line conditional on PQR defined #else second line conditional on PQR not defined #endif third line ]; my $macro = 'ABC'; $str =~ s[ \#ifdef\s(\S+)\s+ ([^\n]+)\n \#else\s+ ([^\n]+)\n \#endif ]{ $1 eq $macro ? $2 : $3 }xeg or die 'No match'; print $str; __END__ C:\test>junk7 first line second line conditional on ABC defined second line conditional on PQR not defined third line

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: assertions help
by DavidFerrington (Acolyte) on Dec 22, 2009 at 19:48 UTC
    Hmmm, thanks to people for replies, something for me to try. In the last case, I think that only allows for one line between the #ifdef and the #else and likewise between the #else and the #endif, but in relality, there can be many lines??
    -- David

      Update:Tweaked to prevent extraneous newlines.

      Good examples are everything:

      #! perl -slw use strict; my $str = q[ first line #ifdef ABC second line conditional on ABC defined stuff more stuff #else second line conditional on ABC not defined other stuff and maybe some more and a bit more #endif #ifdef PQR second line conditional on PQR defined #else second line conditional on PQR not defined #endif third line ]; my $macro = 'ABC'; $str =~ s[ \#ifdef\s(\S+)\s+ ( (?: [^\n]+ \n )*? [^\n]+ ) \n \#else\s+ ( (?: [^\n]+ \n )*? [^\n]+ ) \n \#endif ]{ $1 eq $macro ? $2 : $3 }xeg or die 'No match'; print $str; __END__ C:\test>junk7 first line second line conditional on ABC defined stuff more stuff second line conditional on PQR not defined third line

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.