On Mon Mar 19 19:16:43 2018, damian@conway.org wrote:
Show quoted text> Hi Alex,
>
> Thanks for the report, but (in my view) this is not a bug;
> it's the defined behaviour...albeit, perhaps a surprising one.
>
> You're assuming that:
>
> <[MATCH=decl]>*
>
> is a shorthand for:
>
> <[decl]>* <MATCH=(?{ $MATCH{decl} })>
>
> Although that is entire plausible, it's not the case.
> It's actually a shorthand for:
>
> (?: (<decl>) (?{ $MATCH = [] unless ref($MATCH) eq 'ARRAY'; push
> @$MATCH, $CAPTURE }) )*
>
> In other words, each time the <[decl]> subtoken matches,
> what it matches is pushed onto the $MATCH, converting it
> to an arrayref on the first occasion.
>
> But when the zero-or-more quantifier matches zero times,
> the trailing code is never executed, so the default value of $MATCH
> (an empty string) is never modified.
>
> I don't propose to change that behaviour, but I will add a caveat
> to the documentation pointing it out.
>
> Meanwhile, the workaround is to set up the default return value you want
> yourself, either using the variant you included in your report, or else
> with:
>
> (?{ $MATCH = [] })
> <[decl]>*
>
> I'm sorry for the trouble this caused you.
>
> Damian
Hi Damian,
Thank you for the default value tip, I didn't realize it was possible to do something like that!
With regards to $MATCH returning an empty string when the zero-or-like quantifier matches zero times, why does something like this:
<nocontext:>
<token: decls>
<[decl]>*
<token: body>
<decls>
result in <[decl]> being undef when it matches zero times, while the default $MATCH for <[decl]> is an empty string when it matches zero times?