http://www.perlmonks.org?node_id=1029726

7stud has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

Can someone point me to some documentation where it says it's legal to have a string on the right side of the match operator?

use strict; use warnings; use 5.016; my $str = 'hello'; if ($str =~ 'he') { say 'yes'; } --output:-- yes

Replies are listed 'Best First'.
Re: string on right side of =~?
by Athanasius (Archbishop) on Apr 21, 2013 at 07:24 UTC
Re: string on right side of m//?
by moritz (Cardinal) on Apr 21, 2013 at 09:35 UTC

    It's deeply ingrained in Perl that the operators dictate what to do, and not the type of the operands (with few exceptions).

    So =~ always does a regex match. If the right-hand side isn't a regex, it becomes one.

    Same with . (coerces the arguments to be strings), + (coerces to numeric) etc.

    Update: since the question was about documentation, I checked perlop if this was explained somewhere. I didn't find anything, so I've submitted a patch which adds such an explanation.

Re: string on right side of m//?
by BrowserUk (Patriarch) on Apr 21, 2013 at 07:37 UTC

    It is also legal to use a number or another variable wherever you might use a regex:

    $s = '0123456789';; print $s =~ 0;; 1 $r = 123;; print $s =~ $r;; 1

    Whatever you put there, perl will try to resolve as a regex.

    The downside is that you can't use modifiers without // or m//.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    div class=
Re: string on right side of m//?
by 2teez (Vicar) on Apr 21, 2013 at 09:46 UTC

    with use 5.016, you don't need use warnings; use strict; Just saying....

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me