3

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.

Sridhar
  • 31
  • 1
  • 1
  • 4
  • http://stackoverflow.com/questions/619926/should-i-escape-shell-arguments-in-perl – xxfelixxx May 27 '16 at 09:55
  • 3
    Why are using backtick with system command? You should use `" "` like `system(" ")` . – serenesat May 27 '16 at 10:16
  • Thanks for your reply. system("perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl"); This I have already tried but of no use,. I still get the same error. Please suggest a fix. I will try and update you – Sridhar May 27 '16 at 10:19
  • 1
    If you have tried something and doesn't work, try to fix it but for that do not use wrong syntax. – serenesat May 27 '16 at 10:33
  • Make it more readable, please use [`quotemeta`](http://perldoc.perl.org/functions/quotemeta.html) and any other delimiter than `/` like this `s{\Q/$}{\Q\$}g` – Arunesh Singh May 27 '16 at 10:48
  • Thank you guys for helping me out .. :D – Sridhar May 27 '16 at 11:16

2 Answers2

0

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.

gaganso
  • 2,914
  • 2
  • 26
  • 43
  • Thanks for your reply. When I am using System Function like you have specified, I am getting the below error "syntax error at flat_rtl_gen.pl line 175, near "system" Execution of flat_rtl_gen.pl aborted due to compilation errors." When I used Back Ticks, I am getting The below Error "Backticks found where operator expected at flat_rtl_gen.pl line 177, near "`"perl -p -i -e 's/\/\$/\/\\\$/g' sed_list_gen.tcl"`" (Missing semicolon on previous line?) syntax error at flat_rtl_gen.pl line 177, near "`"perl -p -i -e 's/\/\$/\/\\\$/g' sed_list_gen.tcl"`" Please Suggest . . – Sridhar May 27 '16 at 10:36
  • I tried out and didn't find any errors. Can you please post flat_rtl_gen.pl? Also do "perl -c flat_rtl_gen.pl" and post the result. Please edit the question to add these details. – gaganso May 27 '16 at 10:45
  • Hi silent Monk, the below code worked `perl -p -i -e 's/\\\/\\\$/\\\/\\\\\\\$\g' file`; I am really thank ful to you for your time. I will try out your code and will update you .. – Sridhar May 27 '16 at 11:04
  • I can not post flat_rtl_gen script as it is proprietary script. When I did "perl -c flat_rtl_gen.pl $PWD". I got a msg saying that Syntax OK. But when I am running with the fix that you have given, I am still getting an error saying that "Syntax error at -e, near "s//$/" " – Sridhar May 27 '16 at 11:08
  • Thanks once again for your time :D – Sridhar May 27 '16 at 11:16
  • 1
    The problem with the first statement here is that your system command is using double quotes, which causes each backslash to be treated as an escape character *before* the string is passed to the argument of `perl -p -i -e`. As a result you'd have to double-backslash all of the meta characters. You can get around it by just using non-interpolating quotes: `system(q{perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl});` – Paul L May 27 '16 at 14:08
0

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. . .

Sridhar
  • 31
  • 1
  • 1
  • 4
  • 1
    My goodness that's disastrously messy. You can take two steps to clean it up. 1) Use non-interpolating quotes for your `system()` command. 2) Use a delimiter other than `/` for your search-and-replace: `system(q{perl -p -i -e's!/\$!/\\$!'});` – Paul L May 27 '16 at 14:12