
rovf
Veteran
Mar 8, 2013, 12:15 AM
Post #2 of 7
(225 views)
|
|
Re: [abhi] unterminated `s' command
[In reply to]
|
Can't Post
|
|
Backquotes (``) treat the part between them like a double quoted string, which means that the backslash (\) is seen by Perl as escape character. You can easily see, what command is actually passed in a shell, by replacing the backquote execution by a print statement (the qq(...) form is just an alternative way for writing a double-quoted string:
use strict; use warnings; my $g_name='xyz'; my $file_name='abcd'; print qq(sed -i "s/$g_name/$g_name \-name \"replace NUMBER\#1234\"/g" $file_name ); If you run this, you get the following output: sed -i "s/xyz/xyz -name "replace NUMBER#1234"/g" abcd As you can see, this is indeed a unterminated sed-command. Three more remarks: (1) With your usage of backquotes, you just collect the stdout of the command and throw it away immediately. In this case, it makes more sense if you redirect it to the bit bucket from the beginning:
system(qq(sed .... >/dev/null)); See perldoc -f system for an explanation of the system function. (2) It doesn't make much sense to shell out to sed for processing a file, since you can do this easily in Perl too. There are several ways to do it, but from the code posted, it is not clear to me what you want to achieve (i.e. what you want to do with the transformed text). (3) Please always start your program with
use strict; use warnings; While you are still learning Perl, I even suggest to use
use strict; use warnings FATAL => qw(all); use diagnostics; instead, though I admit that not everyone would agree in this.
(This post was edited by rovf on Mar 8, 2013, 12:17 AM)
|