Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: pattern matching: why does the following code evaluate true? (and how do i fix it?)

by Buckaroo Buddha (Scribe)
on May 31, 2000 at 02:57 UTC ( [id://15540]=note: print w/replies, xml ) Need Help??


in reply to pattern matching: why does the following code evaluate true? (and how do i fix it?)

so it should be:
($key =~ m/.$key2/)
i was just about to follow up with
($key =~ m/^$key2$/)
as being how i solved this problem ...

(well how O'Reilly solved it)

i geuss this means that the '.' in the string is interpreted as a wildcard matching to the end of the line?

that's interesting ... please correct me if i'm wrong but if that's the way it works that's a really cool thing to have learned

:)!

  • Comment on Re: pattern matching: why does the following code evaluate true? (and how do i fix it?)
  • Select or Download Code

Replies are listed 'Best First'.
RE: Re: pattern matching: why does the following code evaluate true? (and how do i fix it?)
by reptile (Monk) on May 31, 2000 at 03:17 UTC

    The '.' matches a single character, any character, except \n (unless using the 's' modifier, to treat the expression as a single line). There are two ways to solve the problem, one is how you did it, and another is to use /\Q$key2/ as others have said, so the '.' is treated as a literal '.' (along with every other metacharacter).

    $key =~ m/^$key2$/; # works $key =~ m/\Q$key2/; # also works

    Personally I think the second option is better here. The '.' normally matches the literal '.' and anything else as well, but if your $key contained something like "INITIAL!LASTNAME" it would match if "INITIAL.LASTNAME" where the pattern. Quoting metacharacters with \Q$pattern\E is, at least, more correct, even if the first will work ok with the given input. It'll help you avoid bugs to use the second option.

    72656B636148206C72655020726568746F6E41207473754A

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://15540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found