http://qs321.pair.com?node_id=1135015


in reply to How to handle metacharacters in input file for Perl one-liner code

Not being a windows person, I don't know if the \Q\E quotemeta that marinersk is suggesting will have problems when passed to the windows shell, but I'm fairly certain that there would be at least some risk that you might eventually need to include a character that causes some pain due to shell escaping. Thus, I beg you consider doing it the long way.

my $file = "foo.c"; rename "sample.csv", "sample.csv.bak" or die "Error backing up sample. +csv: $!"; open my $IN, "<", "sample.csv.bak" or die "Error reading sample.csv +.bak: $!"; open my $OUT, ">", "sample.csv" or die "Error writing to sample. +csv: $!"; while (defined(my $line = <$IN>)) { $line =~ s/\Q$file\E/$file,=SUM(B$x:B$y)/; print $OUT $line; }

Good Day,
    Dean

Replies are listed 'Best First'.
Re^2: How to handle metacharacters in input file for Perl one-liner code
by debug (Initiate) on Jul 16, 2015 at 14:12 UTC

    Hello Dean,

    Thanks for your reply.

    I should probably have mentioned that I'm running this code inside an iterative loop, so am a bit concerned with execution time. I did consider going this approach instead of a system call, but honestly I have no idea which one would be a more cpu intensive process in terms of file open operations. Would you have any thoughts on that?

      Shelling out to the OS is a very expensive process. If you're really concerned about execution time, do it all in Perl.


      Give a man a fish:  <%-(-(-(-<

      A pure Perl solution is likely to be faster than shelling out to the OS.