in reply to about style: use possessive or atomic?

Isn't the second one a redundant way for writing

   /($match)+/x

?

update

No it's not, see my code further down.

update thread overview

Thread Tl;dr? After some confusion we identified a bug which was introduced with 5.20.

See Re^8: about style: use possessive or atomic? (BUG!!!)

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

Replies are listed 'Best First'.
Re^2: about style: use possessive or atomic?
by BrowserUk (Patriarch) on Aug 16, 2015 at 11:16 UTC
      chomp( $match = <DATA> ); chomp( $_ = <DATA> ); for my $regex ( "($match){1}+", "($match)+", "($match)+?", ){ print qw(FAIL SUCCESS)[ !! m/\A $regex \z/x ], "\n" } __DATA__ (a|b){2} bbab __OUTPUT__ FAIL SUCCESS SUCCESS
      Is it true that ($match){1}+ equals to simply $match?

        Hello rsFalse,

        Is it true that ($match){1}+ equals to simply $match?

        Yes, and the + does nothing here, as you can see with use warnings:

        Output:

        22:21 >perl 1345_SoPW.pl Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/\A ((a|b){2}){1}+ <-- HERE \z/ at 1345_SoPW.pl line 27, <DATA> li +ne 2. FAIL FAIL FAIL SUCCESS SUCCESS 22:21 >

        Hope that helps,

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

        The whole discussion is quite confusing because greediness and atomic grouping are messed up.

        Could you please show a use case where you want to avoid backtracking into a once matched group (thats what atomic/possessive is about)

        answering your question

        > Is it true that ($match){1}+ equals to simply $match?

        No!

        DB<125> $_ = "a"x5 => "aaaaa" DB<126> $match = "(a+)" => "(a+)" DB<128> $_ =~ /${match}a/ => "aaaa" DB<129> $_ =~ /${match}{1}a/ => "aaaa" DB<130> $_ =~ /${match}{1}+a/ DB<131> $_ =~ /(?>${match})a/ DB<132>

        It seems to show that perlre is wrong with the redundant anotation.¹

        edit

        Answering your root question, IMHO using + as a second quantifier like in a++ only makes sense if you already need to use a first quantifier in the first level of interpolation, like in

        ($pat1)++($pat2)*+

        i.e. + and * are not part of a sub-pattern $patN.

        Otherwise I'd always prefer

        (?>$pat1)(?>$pat2)

        using something like ($pat){1}+ seems quite confusing for me.

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

        ¹) if "redundant" is supposed to mean line 131 and 130 are equivalent, then redundant is a misleading wording

      I just ignored that the + is a possessive quantifier in this combination.

      But its still redundant like the docs you cited say, backtracking wouldn't change without the plus. 1

      (as far as I understand the docs with my pre coffee brain and without possibility to test on my mobile)

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

      1) no that's not true, see discussion further down!