in reply to Search and Replace Within Subscopes

Like this?

#! perl -slw use strict; my $data = do{ local $/; <DATA> }; $data =~ s[(if.*?{)]{ ( my $subscope = $1 ) =~ s[\n][]g; $subscope; }smge; print $data; __END__ if (@Agent eq 'XX') { @Agent='{XX}' } if ( @Agent eq 'XX' ) { @Agent='{XX}' }

Prints

C:\test>junk7 if (@Agent eq 'XX'){ @Agent='{XX}' } if ( @Agent eq 'XX') { @Agent='{XX}' }

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Search and Replace Within Subscopes
by DanielNeedles (Novice) on Jul 04, 2008 at 16:39 UTC
    Looks good, but some of the syntax is a bit alien to me (in particular the use of [] and {} in '$x=~ s.{.}smge' Is there a good text/url on this kind of syntax?
      Those are alternate delimeters. See the "substitution operator" in perlop. Also read perlre. There is a wealth of information on perldoc.perl.org.
      You can use any pair of non-alphanumeric, non-whitespace charaters as delimiters of a regex pattern. So these regexes are equivalent:

      s/a/b/; s"a"b"; s^a^b^; s{a}{b}; s(a)[b];
      Only ' is an exception, this character prevents variable interpolation if used as delimiter

      Advantage is that you can use / inside them without escaping the character. Also it could be argued that {} [] or () as delimiters just look better, especially for complex patterns. Edit: Deleted one erraneous example found by ikegami

        >perl -ce"/a/b/" Bareword found where operator expected at -e line 1, near "/a/b" (Missing operator before b?) syntax error at -e line 1, near "/a/b" -e had compilation errors.

        /a/b/
        is the same as
        m/a/ b /