Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by duelafn (Parson)
on Jul 16, 2015 at 13:15 UTC ( [id://1135015]=note: print w/replies, xml ) Need Help??


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?

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

      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:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1135015]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found