Sridhar
Sridhar

Reputation: 31

Perl Search and Replace Command Not Working When Called from Inside another Perl Script

I am a beginner with Perl. I am using the Below Perl Command To Search and Replace "/$" Sequence in my tcl Script. This works well When used on the linux Command Line directly.

perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl

I am calling to Call the above Perl One liner in another Perl script using System Command and only with " ` " Back Tick.

system(`perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl`);
`perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl`;

I am getting the Below error. Please Help With this issue.

Bareword found where operator expected at -e line 1, near "$/g"
(Missing operator before g?)
Final $ should be \$ or $name at -e line 1, within string
syntax error at -e line 1, near "s//$/"
Execution of -e aborted due to compilation errors.

I Dont Know if I Can use any other Separation Operator like % and # just like SED command but, When I used '%' operator for separation, I didn't see error but job is not done.

`perl -p -i -e 's%\/\$%\/\\\$%g' sed_list.tcl`; 

I couldn't find sufficient results for this particular issue of '$' variable on the web. Any help is appreciated.

Upvotes: 3

Views: 962

Answers (2)

gaganso
gaganso

Reputation: 3011

You can execute an external command by passing the command to a system function or by using backticks(``) operator. Please pass the command to the system() function as a string:

system(q{perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl})

or use backticks as:

`perl -p -i -e 's/\/\$/\/\\\$/g' sed_list_gen.tcl`

Edit: As suggested by Paul in the comments.

Upvotes: 0

Sridhar
Sridhar

Reputation: 31

Some one here Suggested that I should Escape all Back Slashes while using System Command or calling another command using BackTicks from inside a perl script. But later they have deleted their answer. It worked for me. I would like to thank every one for taking effort and helping me out in solving my question.

Here is the correct working code.

`perl -p -i -e 's/\\\/\\\$/\\\/\\\\\\\$/g' sed_list_gen.tcl`;

or Use System function as shown Below

system("perl -p -i -e 's/\\\/\\\$/\\\/\\\\\\\$/g' sed_list_gen.tcl");

Thanks once again for the community for helping me out. . .

Upvotes: 0

Related Questions