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

Regex without 'm' or '/'

by dvergin (Monsignor)
on Jul 10, 2002 at 01:30 UTC ( [id://180659]=note: print w/replies, xml ) Need Help??


in reply to Am I on the pipe, or what?

The example that BorgCopyeditor supplies with his question brings up an interesting point. Why does this work:      if($query=~$term) {...} Or, to offer a very plain example, why does this work:
if ( 'abc' =~ 'bc' ) { print "yes\n"; }
The rule I learned was (quoting from perlop): "If "/" is the delimiter then the initial m is optional."

Fair enough. The implication is:

if ( 'abc' =~ m/bc/ ) { # good if ( 'abc' =~ /bc/ ) { # good if ( 'abc' =~ m%bc% ) { # good (for any non-alphanum) but: if ( 'abc' =~ 'bc' ) { # BAD! (or so we assume)
But it is not so. As I said above, both of the examples at the top of this response work. Why?

What I cannot find in the on-line docs (update: see danger's response below) but I know from experimentation and the words of Camel 3, page 144, is that, even without the 'm' or the '/', the righthand side of =~ "still counts as a m// matching operation, but there'll be no place to put any trailing modifiers, and you'll have to handle your own quoting."

So...

if ( 'abc' =~ 'bc' ) { # works if ( 'abc' =~ $pattern ) { # works if ( 'abc' =~ "$pattern" ) { # works if ( 'abc' =~ bc ) { # works!! if ( 'abc' =~ 'bc'g ) { # Error: bareword 'g'
The present writer is not responsible for any sideways looks any of these may earn you from your peers.

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall

Replies are listed 'Best First'.
Re: Regex without 'm' or '/'
by danger (Priest) on Jul 10, 2002 at 04:05 UTC
    What I cannot find in the on-line docs

    Its an operator thing, not a regex thing --- from perlop under "Binding Operators":

    ... If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time. This can be less efficient than an explicit search, because the pat- tern must be compiled every time the expression is evalu- ated.

    This can sometimes cause problems for newcomers, especially when they use split with a double-quoted string as the split pattern (as seems to happen with undue frequency) and have an escaped metacharacter in the pattern:

    $_ = 'this has a | pipe'; @a = split /\|/; # good print join(":", @a),"\n"; @a = split "\|"; # oops print join(":", @a),"\n";

    In the second case, the double-quoted string is first evaluated and the *resulting* string (sans backslash) is then used as the pattern in the regex.

      If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time. This can be less efficient than an explicit search, because the pat- tern must be compiled every time the expression is evalu- ated.

      This perlop text must be a holdover from a while back. At least as far back as 5.005_03, code like $str1 =~ 'bc' (with a constant string for the pattern) would be compiled only once. Between 5.6.0 and 5.6.1 an extra check was added, so that even $str =~ $str2 would not be recompiled as long as $str2 had not changed.

      I guess the second statement should simply be deleted from that paragraph.

      Hugo
Thanks, all
by BorgCopyeditor (Friar) on Jul 10, 2002 at 02:03 UTC

    Thanks to Texas Tess, Zaxo, and dvergin for patient explanations of both the obvious and the arcane. Also, next time I have a regex problem, maybe I'll brave the debugger. That was very enlightening.

    FWIW, the data I'm parsing is in 'betacode', an ASCII transcription scheme for Ancient Greek. It's convenient in some ways, but chock full of what I still have to remind myself are metacharacters. Grrr.

    BCE
    --Your punctuation skills are insufficient!

Log In?
Username:
Password:

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

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

    No recent polls found