0

I am using this command to perform find and replace

system( "perl -pi -e 's/$arr3[$i]/$arr2[$i]/g' /opt/app/d1ebl1m5/dv02/cingbt02/J2EEServer/config/AMSS/application/properties/CSCEnvVar.properties_try"); 

the value of $arr3[$i] contains special characters like -e, $ etc. what can I do to ignore all special characters and just treat the expression as normal string

1
  • 2
    Using a system command to call Perl within Perl is rather inefficient and slow. Especially if this operation is run multiple times, as it seems that it is (looping over @arr3 and @arr2). It would most likely be 10 times faster to just open the files normally. Commented Oct 7, 2014 at 13:49

1 Answer 1

2

You normally want

my $search_pat = quotemeta($search); s/\Q$search_pat\E/$replace/g 

or the equivalent

s/\Q$search\E/$replace/ 

It's usually a bad idea to generate code, so the solution becomes

system( perl => ( '-i', '-p', '-e' => ' BEGIN { $s = shift(@ARGV); $e = shift(@ARGV); } s/\Q$s/$e/g ', '--', $arr3[$i], $arr2[$i], '/opt/app/.../CSCEnvVar.properties_try', ) ); 
Sign up to request clarification or add additional context in comments.

6 Comments

Is -- absolutely necessary and shouldn't be -i => -p =>?
Yes, otherwise it will fail if $arr3[$i] starts with - (which the OP specifically said is possible) // Why? -p is not a value for -i.
They should be quoted then as they are system() string arguments.
@mpapec, Hum, -i doesn't need to be quoted, but -p does??? perl -E'use strict; say for -i, -p'. Fixed.
-p is test for named pipe, and -e gets quoted anyway due fat comma . Don't know why -i doesn't need quotation. +1, perhaps implicit @ARGV when shifting.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.