Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

mysterious regexp syntax

by grizzley (Chaplain)
on Feb 04, 2014 at 08:40 UTC ( [id://1073347]=perlquestion: print w/replies, xml ) Need Help??

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

I have following entry in some script
# Find LOGIN tag. if ((($ln =~ ?<[ ]*LOGIN[ ]?) || ($ln =~ ?<[ ]*LOGIN$?)) && ($pword) && ($uname)) { ... }
It is working, but how? I'm sure match should start with 'm' letter if delimiter is not '/', but this '?' clearly is not delimiter. It is not assertion as well because assertion starts with '?<='. Looks like '<' is just part of pattern searching for '<LOGIN' (with optional white characters). '?' is special character and perl should give me some syntax error like:
C:\>perl -e "/?/" Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HER +E / at -e line 1.
and yet it just works. Deparse does not give any clue:
# perl -MO=Deparse -e '$ln=" < LOGIN > "; $ln =~ ?<[ ]*LOGIN[ ]?' $ln = ' < LOGIN > '; $ln =~ ?<[ ]*LOGIN[ ]?; -e syntax OK
neither does Regex::Explain:
C:\>perl -e "use YAPE::Regex::Explain; $REx = '?<[ ]*LOGIN[ ]?'; print + $exp = YAPE::Regex::Explain->new($REx)->explain" The regular expression: matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- C:\>perl -e "use YAPE::Regex::Explain; $REx = qr/?<[ ]*LOGIN[ ]?/; pri +nt $exp = YAPE::Regex::Explain->new($REx)->explain" Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HER +E <[ ]*LOGIN[ ]?/ at -e line 1.
Perl version is 5.8.8 on Linux and 5.14.2 on Windows.

Replies are listed 'Best First'.
Re: mysterious regexp syntax
by Tux (Canon) on Feb 04, 2014 at 08:46 UTC
    $ man perlop : : If "/" is the delimiter then the initial "m" is optiona +l. With the "m" you can use any pair of non-whitespace (ASCII) characters as delimiters. This is particularly useful +for matching path names that contain "/", to avoid LTS (lea +ning toothpick syndrome). If "?" is the delimiter, then a m +atch- only-once rule applies, described in "m?PATTERN?" below +. If "'" is the delimiter, no interpolation is performed on +the PATTERN. When using a character valid in an identifier +, whitespace is required after the "m". : : m?PATTERN?msixpodualgc ?PATTERN?msixpodualgc This is just like the "m/PATTERN/" search, except that +it matches only once between calls to the reset() operator +. This is a useful optimization when you want to see only the +first occurrence of something in each file of a set of files, + for instance. Only "m??" patterns local to the current pa +ckage are reset.

    Enjoy, Have FUN! H.Merijn
      Ahhhh... So possibility to omit 'm' remains in case of '?' delimiter. That is what I've been missing.
      # perl -le '$_="abc"; print 1 if ?b?'
      Thanks for clarification!

        But note the following, from perl5140delta:

        ?PATTERN? (without the initial m) has been deprecated and now produces a warning. This is to allow future use of ? in new operators. The match-once functionality is still available as m?PATTERN?.

        So with v5.14.4, I get:

        22:39 >perl -le "$_ = qq[abc]; print 1 if ?b?;" Use of ?PATTERN? without explicit operator is deprecated at -e line 1. 1 22:39 >

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found