in reply to Disabling Variable Expansion in regular expressions.

> I need a way to disable the variable expansion, while preserving the interpretation of regular expression metacharacters.

use single quotes as delimiter ... it's magic!

DB<17> $a= "xxx" DB<18> p 'xxx' =~ m/$a/ 1 DB<19> p 'xxx' =~ m'$a' DB<20>

From perlre#The-Basics

> Most times, the pattern is evaluated in double-quotish context, but it is possible to choose delimiters to force single-quotish, like

$foo =~ m'abc'

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re: Disabling Variable Expansion in regular expressions. (single quotes as delimiters)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Disabling Variable Expansion in regular expressions. (single quotes as delimiters)
by LanX (Saint) on May 23, 2019 at 16:28 UTC
    hmm ... either it's buggy or I'm doing something wrong

    $ perl print '$x' =~ m'$x' __END__ $

    EDIT:

    darn, the $ becomes an anchor for end-of-line

    DB<8> use re 'debug'; '$a' =~ m'$a' Compiling REx "$a" Final program: 1: SEOL (2) 2: EXACT <a> (4) 4: END (0) anchored "a" at 0 (checking anchored) minlen 1

    unfortunate!

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Rolf

      Sadly, my testing shows this is a valid indicator. Perl had no trouble with a pattern that had text past the end of string indicator. Though this is the mechanism I was looking for, it didn't produce the result I expected.

      Thank you so much for your help. It was exactly what I wanted, just not what I needed.

      Rolf,

      But that is precisely the outcome I am looking for. I want to detect the improper use of the $. This hard quoted form seems to be the answer I am looking for.