Search and replace question

L

laredotornado

Hi,

I'm using Perl 5.10.6 on Mac 10.6.6. I want to execute a simple
search and replace against a file ...

my $searchAndReplaceCmd = "perl -pi -e 's/\\Q${localTestDir}\\E//g' $
{testSuiteFile}";
system( $searchAndReplaceCmd );

but the problem above is the variable $localTestDir contains directory
separators, and this screws up the regular expression ...

Bareword found where operator expected at -e line 1, near "s/\Q/home/
selenium"
Backslash found where operator expected at -e line 1, near "Live\"
syntax error at -e line 1, near "s/\Q/home/selenium"
Search pattern not terminated at -e line 1.

How do I do a search and replace when the variable in questions
contains regular expression characters? Thanks, - Dave
 
U

Uri Guttman

l> I'm using Perl 5.10.6 on Mac 10.6.6. I want to execute a simple
l> search and replace against a file ...

l> my $searchAndReplaceCmd = "perl -pi -e 's/\\Q${localTestDir}\\E//g' $
l> {testSuiteFile}";
l> system( $searchAndReplaceCmd );

why are you calling out to a perl subprocess when you are already inside
perl? you can just read in the file, edit it and write it out again. the
-pi options aren't so useful when you just need to do that inside
perl. it is obviously causing you problems due to shell interpretation
and such. it also means your regex is more cluttered than it needed to
be.

l> How do I do a search and replace when the variable in questions
l> contains regular expression characters? Thanks, - Dave

well, you can use perl to read/edit/write the file directly. you can use
File::Slurp to make that even easier. then again, you can use
File::Slurp's edit_file_lines sub to do it even more easily.

use File::Slurp qw( edit_file_lines ) ;

edit_file_lines { s/$localTestDir//g } $test_file ;

and since you are doing the s/// globally, you can speed it up with
edit_file which slurps in the whole file into $_:

use File::Slurp qw( edit_file ) ;

edit_file { s/$localTestDir//g } $test_file ;


done. no need to escape anything or worry about shell stuff or several
other issues you had with your code.

uri
 
J

John W. Krahn

laredotornado said:
Hi,

I'm using Perl 5.10.6 on Mac 10.6.6. I want to execute a simple
search and replace against a file ...

my $searchAndReplaceCmd = "perl -pi -e 's/\\Q${localTestDir}\\E//g' $
{testSuiteFile}";
system( $searchAndReplaceCmd );

but the problem above is the variable $localTestDir contains directory
separators, and this screws up the regular expression ...

Ooh, how to run perl code from inside perl? I think I know that one:

{ local ( $^I, @ARGV ) = ( '', $testSuiteFile );
while ( <> ) {
s/\Q$localTestDir\E//g;
print;
}
}



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top