When you don't provide a valid operator (m//, s/// or tr///) on the right-hand side of the binding operator (=~), it's presumed you meant to use a match operator (m//). That means that
$a =~ $b
is short for
$a =~ m/$b/;

There's no reason to expect any of the following to evaluate the Perl code found in $b:

$p = '4+5*6'; $str =~ m/$p/; # Doesn't perform arithmetic. $p = 'm/a/'; $str =~ m/$p/; # Doesn't search for `a`. $p = 's/a/b/'; $str =~ m/$p/; # Doesn't perform a substitution.

Same:

$p = '4+5*6'; $str =~ $p; # Doesn't perform arithmetic. $p = 'm/a/'; $str =~ $p; # Doesn't search for `a`. $p = 's/a/b/'; $str =~ $p; # Doesn't perform a substitution.

qr// compiles a regular expression, not Perl code. It doesn't help at all here.

$p = '4+5*6'; $re = qr/$p/; $str =~ m/$re/; # Still doesn't. $p = 'm/a/'; $re = qr/$p/; $str =~ m/$re/; # Still doesn't. $p = 's/a/b/'; $re = qr/$p/; $str =~ m/$re/; # Still doesn't.

Same:

$p = '4+5*6'; $re = qr/$p/; $str =~ $re; # Still doesn't. $p = 'm/a/'; $re = qr/$p/; $str =~ $re; # Still doesn't. $p = 's/a/b/'; $re = qr/$p/; $str =~ $re; # Still doesn't.

have to write like this

Yes, if you have Perl code you wish to compile and run, you will need to pass it to eval EXPR, do, require, use, or to the perl program itself.


I used to think qr take care of all regex expressions

You're not trying to "take care of a regex expression", whatever that means. You are trying to perform a substitution. That requires the use of the substitution operator (s///), but you used a match operator (m//) in your broken code.

qr// compiles a regular expression. That's it. It doesn't perform any matches or substitutions. To perform matches or substitutions, you still need Perl code that uses the match operator (m//) or the substitution operator (s///).


Proper use:

my $pat = 'a'; my $re = qr/$pat/; $str =~ m/$re/ # Performs a match. $str =~ /$re/ # Same $str =~ $re # Same $str =~ m/$pat/ # No need to compile in advance. $str =~ /$pat/ # Same $str =~ $pat # Same
my $pat = 'a'; my $repl = 'b'; my $re = qr/$pat/; $str =~ s/$re/$repl/ # Performs a substitution. $str =~ s/$pat/$repl/ # No need to compile in advance.

Use String::Substitution if the replacement string contains $1, etc.


In reply to Re: surprised to find cannot assign s/../../ to a variable by ikegami
in thread surprised to find cannot assign s/../../ to a variable by vincentaxhe

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.