Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: program cant find values that start with $

by jcb (Vicar)
on Jan 31, 2020 at 05:08 UTC ( #11112155=note: print w/replies, xml ) Need Help??

in reply to Re: program cant find values that start with $
in thread program cant find values that start with $

There are two different layers of processing here. First, Perl applies the standard interpolations for double-quoted ("qq") strings to get a regex. Second, Perl interprets the string it produced in the first step as a regex.

The $ character is special in both of these contexts: in qq-string interpolation it introduces a variable substitution and in a regular expression (from which Perl regexes inherit their basic features) it is a special atom matching "end of line". The problem in this program is that we need to interpolate a string obtained from Tk such that when the user enters "abc$def", the regex engine will actually match that exact string, so we need the first step to produce 'abc\$def' with a backslash to escape the $ character. This is what quotemeta does, and using "\Q$foo\E" is shorthand for my $tmp = quotemeta $foo; and "$tmp" but probably more efficient.

A simple \$ prevents the interpolation in the first step. Using "\$foo" happens to be equivalent to '$foo', which would be a fixed search pattern and unlikely to be useful in the questioner's program.

Another option would be to document that the program accepts regexes instead of fixed strings and require the user to enter 'abc\$def' if a match for the literal string 'abc$def' is desired.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2020-10-27 15:27 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (257 votes). Check out past polls.