CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [abhi] unterminated `s' command: Edit Log



rovf
Veteran

Mar 8, 2013, 12:15 AM


Views: 715
Re: [abhi] unterminated `s' command

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:


Code
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:


Code
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


Code
use strict; 
use warnings;


While you are still learning Perl, I even suggest to use


Code
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)


Edit Log:
Post edited by rovf (Veteran) on Mar 8, 2013, 12:17 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives