in reply to Re^4: about style: use possessive or atomic?
in thread about style: use possessive or atomic?

Is it true that ($match){1}+ equals to simply $match? Yes, and the + does nothing here, as you can see with use warnings:

That's only true because you the OP included a quantifier in the embedded regex; and you get 3 fails because your anchors guarantee the regex used couldn't succeed (which doesn't seem like a useful test?)

This shows that {n}+ is a valid quantifier:

use strict; use warnings; chomp(my $match = <DATA>); chomp( $_ = <DATA>); for my $regex ( "($match){1}+", "($match){1}", "($match)", "($match)+", "($match)+?", ) { print qw(FAIL SUCCESS)[ !! m/$regex/ ], "\n" } __DATA__ (?:a|b) bbab
C:\test>junk37 SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

Replies are listed 'Best First'.
Re^6: about style: use possessive or atomic?
by Athanasius (Archbishop) on Aug 16, 2015 at 13:25 UTC

    Hello BrowserUk,

    I still get the warning when the regex isn’t embedded:

    23:15 >perl -wE "my $r = qr{(?:a|b)}; say $r;" (?^u:(?:a|b)) 23:17 >perl -wE "my $r = qr{(?:a|b){1}}; say $r;" (?^u:(?:a|b){1}) 23:17 >perl -wE "my $r = qr{(?:a|b){1}+}; say $r;" Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/(?:a|b){1}+ <-- HERE / at -e line 1. (?^u:(?:a|b){1}+) 23:17 >perl -v This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x +64-multi-thread ...

    I get the same warning with Strawberry Perl v5.20.2, but not with v5.18.2. Is the warning erroneous?

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

      I get the same warning with Strawberry Perl v5.20.2, but not on v5.18.2.

      You're right! My default perl is currently 5.18:

      C:\test>\perl5.18\perl\bin\perl.exe junk37.pl SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS C:\test>\Perl5.20\bin\perl.exe junk37.pl Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/((?:a|b)){1}+ <-- HERE / at junk37.pl line 15, <DATA> line 2. SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS C:\test>\Perl22\bin\perl.exe junk37.pl Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/((?:a|b)){1}+ <-- HERE / at junk37.pl line 15, <DATA> line 2. SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS
      Is the warning erroneous?

      Erm. I was relying on Perl to tell me :)

      The fact that the warning has been recently added suggests it is probably correct.

      That said, the construct is still listed in the 5.22 docs:  {n}+   Match exactly n times and give nothing back (redundant); which basically means your guess is as good as anybody's at this point?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
      > Is the warning erroneous?

      if the meaning of {n}+ hasn't be deprecated or changed its a totally wrong warning.

      Please run my code with 5.20, with same results we should file a bug report.

      edit

      As you can see there its not redundant.

      The plus is neither a greedyness operator here, bc its in second place, its there to stop backtracking.

      update
      Apparently adding the second + turns the whole quantifier {n}+ into a possessive quantifier.

      see here

      Like a greedy quantifier, a possessive quantifier repeats the token as many times as possible. Unlike a greedy quantifier, it does not give up matches as the engine backtracks. With a possessive quantifier, the deal is all or nothing. You can make a quantifier possessive by placing an extra + after it. * is greedy, *? is lazy, and *+ is possessive. ++, ?+ and {n,m}+ are all possessive as well.

      so + is "greedy" and ++ "possessive", {n}+ is analogous to ++ .

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        so I installed 5.20.2 via perlbrew and it's even getting scarier, the behaviour of {n}+ has changed.

        DB<3> print "aaa" =~ /(a+){1}+a/ aa DB<4> print "aaa" =~ /(a+){1}a/ aa DB<5> print $] 5.020002

        while with 5.14 it's still like documented:

        DB<1> print "aaa" =~ /(a+){1}+a/ DB<2> print "aaa" =~ /(a+){1}a/ aa DB<3> print $] 5.014002

        This is worse than an erroneous warning that's a fatal break of backwards compatibility!

        update

        5.20 is backtracking while told otherwise

        And that's the trace from 5.14

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!