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

I copied a script straight out of a perl book - I will not mention the author. When script is run it gives the following warnings: reversed -= operator on line 11 Does anyone know why it gives this warning?
#!/perl/ -w $pat=''; #thing to search for #line 3 while(){ print 'Enter the pattern'; chomp($pat =<stdin>); print 'enter the string:'; chomp($_ = <stdin>); #10 if($_ =- m/$pat/g) { print "true\n"; } else{print "false\n";} }

Replies are listed 'Best First'.
Re: pattern match operator
by halfcountplus (Hermit) on Apr 16, 2012 at 17:21 UTC
    There's a typo in: if($_ =- m/$pat/g)

    The =- should be =~.

    Ps: From now on, wrap your code in <code> tags. See Writeup Formatting Tips,

Re: pattern match operator
by toolic (Bishop) on Apr 16, 2012 at 17:27 UTC
    Others have identified the error. diagnostics can give you a more verbose explanation of warnings:
    (W syntax) You wrote your assignment operator backwards. The = mu +st always comes last, to avoid ambiguity with subsequent unary operat +ors.
    In this case, it does not tell you how to correct your problem, but perhaps it makes you look at your issue from a different angle.
Re: pattern match operator
by MidLifeXis (Monsignor) on Apr 16, 2012 at 17:21 UTC

    On line 11 you have =-, which is what the error/warning is referring to. From the context, I think you meant to use =~.

    --MidLifeXis

Re: pattern match operator
by davido (Cardinal) on Apr 16, 2012 at 21:48 UTC

    In a previous thread we asked if you would please wrap your code in <code></code> tags. ...several times. You've also been /msg'ed on the subject. Now it's time for the public shaming. ;)

    If there's no degree of shame sufficient to motivate you to heed previous advice by using <code></code> tags around your code, and <p></p> tags around your paragraphs, perhaps you could do it in the future as a way of saying thank-you for the free Perl related advice you have gotten here. Writeup Formatting Tips.

    Why, you might ask. You're already getting answers to your questions, after all. Apparently posting without code tags is getting you the desired result. Well, the answer is because it makes it easier for people to read your posts, and requires less work from the Janitors, who may need to edit your posts after the fact to make them legible. We also spend less time trying to decipher clearly formatted posts, and as a result, we tend to provide more thorough and on-target answers. And, frankly, because we prefer it to be done that way. We want you to be a part of the Monastery, and we would rather that as a fellow Perlmonk you take a few minutes to familiarize yourself with how make your posts more legible.


    Dave

Re: pattern match operator
by jwkrahn (Abbot) on Apr 16, 2012 at 21:23 UTC

    In your example:

    #!/perl/ -w $pat=''; #thing to search for #line 3 while(){ print 'Enter the pattern'; chomp($pat =<stdin>); print 'enter the string:'; chomp($_ = <stdin>); #10 if($_ =- m/$pat/g) { print "true\n"; } else{print "false\n";} }

    There is only one "pattern match operator" and that is: m/$pat/g.    And the only problem with that is the use of the /g option, which in this case is superfluous.

    Your problem is that the characters between $_ and m/$pat/g are not a valid perl operator.