Fellow monks, I beseech your wisdom....

I've got a file that contains strings that look like this: ITEM-####, which I would like to transform into something I can use to make Perl check against like items in our database, for example: ITEM-1234.

I thought I might achieve this with something simple like

my $string = qw(ITEM-####); $string =~ s/#/\d/g;
but to no avail. Instead I got a warning: "Unrecognized escape \d passed through."

So then I tried escaping the escape, like so:

my $string = qw(ITEM-####); $string =~ s/#/\\d/g;
But that makes my string look like this: ITEM-\\d\\d\\d\\d, which isn't what I'm looking for.

My last idea was to use qw...

my $esc = qw(\d); my $string = qw(ITEM-####); $string =~ s/#/$esc/g;
But that also got expanded to include two back slashes: ITEM-\\d\\d\\d\\d.

So now I'm stumped. Is there any way I can do this? Li'l help?

Update: FunkyMonk is right, my code does work. I was looking at it in the debugger, and apparently the debugger itself was adding the extra backslash. Thank you both.


In reply to Solved: Escaping Escapes by starX

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.