Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: 'g' flag w/'qr'

by kcott (Archbishop)
on May 29, 2016 at 09:53 UTC ( [id://1164437]=note: print w/replies, xml ) Need Help??


in reply to 'g' flag w/'qr'

G'day perl-diddler,

"So how can I attach the "/g" modifier to my "qr" regex ... ?"
  1. Short answer: you can't so stop trying.
  2. Longer answer: read on ...

The 'g' modifier is used by m// and s/// to direct how a regex is to be used (single match, global substitution, etc.); it does not affect the regex itself. qr// has no 'g' modifier. Here's links to all three (note the modifier lists):

The 'g' modifier is not part of qr//'s syntax and, if used, syntax errors are raised (as expected).

$ perl -wE 'my $re = qr{A}g' Unknown regexp modifier "/g" at -e line 1, near "= " Execution of -e aborted due to compilation errors.

You also can't do it with the re pragma's '/flags' mode:

$ perl -wE 'use re "/g"' Unknown regular expression flag "g" at -e line 1.

See also:

On a side note — related to what you're doing but not the current problem at hand — are you familiar with the '(?<flags>:<pattern>)' regex construct described in perlre: Extended Patterns: (?adluimnsx-imnsx:pattern)? This construct, and qr//'s interpolating, allows you to write something like this:

$ perl -wE 'my ($p, $f) = (A => "x"); my $re = qr{(?$f: $p )}; say $re +' (?^u:(?x: A ))

Now you control the available flags and don't have to worry about qr//'s modifiers.

By the way, you can't add a 'g' modifier using this method either.  :-)

$ perl -wE 'qr{(?g:)}' Useless (?g) - use /g modifier in regex; marked by <-- HERE in m/(?g < +-- HERE :)/ at -e line 1.

— Ken

Replies are listed 'Best First'.
Re^2: 'g' flag w/'qr'
by perl-diddler (Chaplain) on May 30, 2016 at 01:25 UTC
    You said " are you familiar with the '(?<flags>:<pattern>)' regex construct described in perlre: Extended Patterns: (?adluimnsx-imnsx:pattern)? "

    Yes, but I seem to be finding "holes" in my memory...i.e. I only remembered the m{ (?i) <pattern> } form... I take it that the ":<pattern>" part allows the flags to apply only to the pattern after the colon and before the end-of-parens...

    I readily admit not to knowing how to use every feature in perl's "RE's"... some of which I might use in some odd case, but many of which I put in my "tmp" memory because they are experimental.

    Too many features have been "experimental" in perl for too long, and it really gets confusing -- since my conception of "experimental" was something that was being introduced but might not be stable yet -- however -- when something that was intro'd as Xperimental, but then was stable for more than 2 major releases, that really doesn't fall into the category of experimental, but more of of "some developer's personal 'pet feature'", that got introduced, but was never removed when the experiment was "over".

    Some of those features were introduced to fill in "holes" in perl (case statement). The "Switch" module that was part of core before 5.8, was deprecated with the introduction of "given/when" and its documentation was changed recommending it's usage. Trouble is, you had a Core module deprecated that in the deprecation notes told you to use "given/when" instead -- when, w/5.18, many years after 5.8, anything that never had the experimental label removed, generate sometimes fatal diagnostics (if you follow advice in most languages to get rid of all warnings, and then make all warnings "fatal").

    I tended to think that advice applied to computer languages and good-programming practices, in general -- but 5.18 made it clear that those rules didn't apply to perl. ;^/ (*sigh*)

      ... Extended Patterns: (?adluimnsx-imnsx:pattern) ...

      This is the very useful and highly non-experimental "non-capturing group" construct. It is most commonly found in its modest  (?:pattern) form, with no modifiers present. If you haven't already, I suggest you seek the company of this potentially stalwart and faithful regex companion.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found