I've run into a problem with a script I'm developing that among other things changes a delimited files delimiter, 1st checking that the new delimiter does not exist in the data.
Everything works fine unless the delimiter is a regex metacharacter. So if the delimiter is ":" no problem, but if the delimiter is a "|" big problem as the expresion /$n_delim/ is always true when $n_delim =~ /|/
I've tried several approaches but they've all failed as I won't know what the delimiter may be. Most often though the delimiter would be "," or "|" or "\t". I been banging my head against the wall for days with this and I just can't figure it out. The snippet below is from the offending script. For those of you wondering; I do have use strict; and use warnings; at the top.

open(FI, "<", $i_file) or die "Unable to open $i_file: $!"; while (sysread(FI, $_, 2 ** 16)){ if (/$n_delim/){ print "Delimiter: $n_delim already in $i_file\n"; exit 2; } }

Thanks in advance!


In reply to regex problem with metachars by sweetblood

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.