Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

string on right side of m//?

by 7stud (Deacon)
on Apr 21, 2013 at 07:16 UTC ( #1029726=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on string on right side of m//?
Download Code
Re: string on right side of =~?
by Athanasius (Monsignor) on Apr 21, 2013 at 07:24 UTC
Re: string on right side of m//?
by BrowserUk (Pope) 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 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 2teez (Priest) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029726]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2014-07-30 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls