G'day FFSparky,
You seem to have a number of problems.
-
Don't use interpolating double-quotes and then try to manually escape characters within the quoted string
(e.g. "C:\\Dir1\\Dir2\\Dir3"); it's tedious, fiddly and error-prone.
Instead, use non-interpolating single-quotes and don't make any manual modifications (e.g. 'C:\Dir1\Dir2\Dir3').
-
The $search part of s/$search/$replace/ interpolates.
Let Perl escape characters here with quotemeta: s/\Q$search/$replace/.
Note that you're still not doing tedious, fiddly and error-prone manual modifications.
-
The '/eeg' modifiers on the s/// are completely unnecessary.
See "perlre: Modifiers" and
"perlop: Regexp Quote-Like Operators: s/PATTERN/REPLACEMENT/msixpodualngcer".
Consider this example script (pm_11152209_re_backslashes.pl) which is portable:
#!/usr/bin/env perl
use strict;
use warnings;
my $search = 'C:\Dir1\Dir2\Dir3';
my $replace = '';
my $var = 'C:\Dir1\Dir2\Dir3\KeepMe';
$var =~ s/\Q$search/$replace/;
print "\$var[$var]\n";
On Cygwin:
ken@titan ~/tmp
$ perl -v
This is perl 5, version 36, subversion 0 (v5.36.0) built for cygwin-th
+read-multi
...
ken@titan ~/tmp
$ ./pm_11152209_re_backslashes.pl
$var[\KeepMe]
On Win10:
C:\cygwin64\home\ken\tmp>perl -v
This is perl 5, version 26, subversion 3 (v5.26.3) built for MSWin32-x
+64-multi-thread
...
C:\cygwin64\home\ken\tmp>perl pm_11152209_re_backslashes.pl
$var[\KeepMe]
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.