deepjyot has asked for the wisdom of the Perl Monks concerning the following question:

Command:
system(perl -pi -e 's/>//g' filename);
why do i get an error while using this command in a perl script?

Error:

Substitution replacement not terminated at -e line 2.
And when run the command: "perl -pi -e 's/>//g' filename" on the shell, it works perfectly fine....

Can anyone let me know the reason behind this?

thanks in advance...

Replies are listed 'Best First'.
Re: sytem command perl
by Anonymous Monk on Aug 25, 2008 at 05:00 UTC
    Because you have to quote strings
    system q(perl -pi -e 's/>//g' filename);
      better yet, quote each string argument:
      system 'perl', '-pi', '-e', 's/>//g', $filename;
      See? I didn't quote $filename because the variable is already the argument I want to pass to 'perl'.
      But you can even do better: instead of calling another instance of perl, do the job right here:
      CHANGE_FILE: { my $backup = "${filename}~"; rename $filename, $backup; open my $b, '<', $backup; open my $f, '>', $filename; select $filename; s/>//g, print while <$backup> }
      []s, HTH, Massa (κς,πμ,πλ)
        • You should check that open, close & rename succeeded
        • If you're going to use select (and I wouldn't) make sure to restore STDOUT to what it should be when you're finished with it
        • You've confused the use of $filename, $f, $backup and $b in your last two statements.
      system q(perl -pi -e 's/>//g' $filename); If i use a scalar variable instead of the filename or an entire directory then the system hangs... system q(perl -pi -e 's/>//g' $dirname/*); doesn't work.... plz help....