How do I disable or prevent Perl from processing special regex characters such as \, * and ? in a quoted string so I can process them as regular characters?

I'm stuck on this one. I'm pulling data from a couple of sources and amongst the many chunks of information I'm sucking up, the darn thing kept choking. Come to find out there are a couple of "illegal" characters that's causing problems for me. For example, some characters like \, * and ? (amongst others) are triggering problems elsewhere within the code. Unfortunately, I can't just send these characters on their merry way since the some of this data is being converted into files on an NTFS partition.

In other words. I'm slurping down data from files (roughly 30GB worth of data). The data within is digested and new NTFS palatable files are created. Perl did exactly what I wanted it to do with the exception of the file naming convention. To my surprise, Linux happily wrote illegal Windows characters to the NTFS partition causing Windows to balk when trying to do anything with them. In other words, Linux (and Perl) created files such as:
C:\random\location\ab\delta.txt
and
C:\random\location\ab*delta.txt
(Technically, Linux wrote to /mnt/c/random/location/. Windows sees it as C:\random\location\).


Where C:\random\location\ is the full path and ab\delta.txt or ab*delta.txt is the file name. So, thinking I was smart, I just did a s/// for all the illegal characters and just replaced them all with _. That worked until I got to ab\delta.txt and ab*delta.txt where both would be renamed to ab_delta.txt, one overwriting the other. OK, so I tried to be a little smarter and tried to use iteration creating ab_delta-1.txt and ab_delta-2.txt but if Perl dies for any reason, I get a bunch of files ending in -3 -4 -5 and no idea what was what.

OK..... Looking back and my internal file structure, I finally decided to do a bit of substitution making \, * and ? into [bs], [a], [q]. Yaaay! It started working. Until I ran into files that needed to be ab\delta.txt and ab\\delta.txt. I was getting ab[bs]delta.txt. DOH!!

So here's what I've come up with so far (with all the extranous crusty stuff removed):

my $test = 'illegal\characters*example?'; my @illegal = qw(\ * ?); my @legal = qw(bs a q); my $c = 0; foreach my $val (@illegal) { $test =~ s/\Q$val\E/[$legal[$c]]/g; $c++; } print $test."\n";

Nice, it produces what I want: illegal[bs]characters[a]example[q].
However if I modify $test to equal 'illegal\\characters\*example?' I get illegal[bs]characters[bs][a]example[q] when it should read illegal[bs][bs]characters[bs][a]example[q] (note the missing [bs] after illegal). I've been trying all sorts of ideas to supress the \\ being escaped, but I'm stuck.

Please, enlighten me and direct me to the proper solution.


In reply to Disable Regex by SavannahLion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.