Re: [Boston.pm] Is there a modifier on m or s that says the argument is not a regex

Bill Ricker via Boston-pm <[email protected]> Sun, 13 Jan 2019 21:56:24 -0500
Newsgroups gmane.comp.lang.perl.perl-mongers.boston
Message-ID <CAAbKA3UZ=Xb-212i-qvmd+ttEr0bZ4QLn2yo5H=2H9e2yt3vhg@mail.gmail.com>
On Sun, Jan 13, 2019 at 9:29 PM Steve Tolkin via Boston-pm <[email protected]>
wrote:

> Is there a modifier for m  that says the argument is not a regular
> expression?  Similarly for the or the first argument to s?
> I looked and did not find it.
> This seems like it would be useful.
>

Right, not quite, there isn't an m//Q option ,  and   m'pattern' the
equivalent syntax to s''(); only suppresses  $var etc interpolation but not
suppressing RE-ness of the match, which is what you're looking for.

But same effect as fgrep (string match not RE match) is available a few
ways -

* m{\Q:-)\E} ; # matches literal smiley as \Q...\E quotes all metachars
betwixt even if /x'd
* $string=quotemeta q(string); m{$string}o;
* do_whatever if  s'literal'($&); # evil $&  to make s''() a noop test,
does what you wanted m'' to do, but ouch slow bad bad bad
* ( -1 != index $_, $literal_pattern )

I include s'pat'($&) for humorous value only ... just don't !

The last totally avoids the RE engine. It might even be faster, but I
wouldn't bet against the RE engine's tuning!

The first two are basically equivalent but the first executes quotemeta()
at compile time.
(
The //o option levels the performance playing field mostly ... but you
can't do that in a loop over an array of patterns .
You could do
    @Patterns = map {my $s = quotemeta $_; qr{$s}; } ( "one", "two",
"three", ) ;
if you want to loop over a list of patterns.
)

see
https://perldoc.perl.org/functions/quotemeta.html
https://perldoc.perl.org/functions/index.html



-- 
Bill Ricker
[email protected]
https://www.linkedin.com/in/n1vux