in reply to RESOLVED: Slurping search-replace patterns from a file

... patterns.txt contains:
s/foo/bar/ s/(fo)o/bar$1/

Because your input file appears to contain perl code, I suspect you can get by with eval:

while(my $line = <FILE>) { chomp; eval "\$string =~ $line; 1" or die $@; }

-Paul

Replies are listed 'Best First'.
Re^2: Slurping search-replace patterns from a file
by HKS (Acolyte) on Oct 14, 2008 at 15:52 UTC
    Because your input file appears to contain perl code, I suspect you can get by with eval:

    Unfortunately, it's not quite that simple. I provided an overly simplified example - it's usually not that clean. This is more along the lines of what my file looks like:

    replace { (foo) and stuff } with { bar $1 }

    Which will be programmatically consolidated to:

    s/(foo) and stuff/bar $1/

    I'd like to avoid debating the format, which is why I provided the simplified example.

    All the same, thanks for pointing out that an eval would work in the original situation.

    -HKS

      Eval may still be the way to go though...
      # rip stuff from file eval "$wahtever =~ s/$lhs/$rhs/; 1" or die $@;
      The /ee stuff is essentially the same as that (in the sense that it has to run eval() on the rhs... I fail to see how you'll preserve the meaning of $1 without an eval or /ee.

      -Paul