develooper Front page | perl.perl6.language | Postings from September 2006

Motivation for /<alpha>+/ set Array not Match?

Thread Next
From:
Audrey Tang
Date:
September 22, 2006 07:23
Subject:
Motivation for /<alpha>+/ set Array not Match?
Message ID:
97C57429-5F3C-4F5A-9A85-161BB7A5CD9E@audreyt.org
 From S05:

If a subrule appears two (or more) times in any branch of a lexical
scope (i.e. twice within the same subpattern and alternation), or if the
subrule is quantified anywhere within a given scope, then its
corresponding hash entry is always assigned an array of
C<Match> objects rather than a single C<Match> object.

However, fglock and I both find the "quantified" clause very surprising.
Intuitively:

     /<foo>  bar bar <foo>/

should set $<foo> to an Array with two Match elements.  However:

   /<foo>+/

should set $<foo> to a single Match, with multiple positional Match  
elements, each
one representing one match in the quantified match.  Moreover:

    /<foo> bar bar <foo>+/

should set $<foo> to an Array with two Match elements, the first being a
simple match, and the second has multiple positional submatches.

The thinking behind the separate treatment is that in a contiguous  
quantified
match, it does make sense to ask the .from and .to for the entire  
range, which
is very hard to do if it's an Array (which can have 0 elements,  
rendering $<foo>[-1].to
dangerous).  Also stringification for $<foo> on a /<foo>+/ match  
should perhaps
not be space-separated, i.e. it should follow Match semantics not  
Array semantics.

To recap: Is it possible to amend the quantified clause to have it  
produce Match
objects, and reserve Array only for noncontiguous same-named subrules?

Thanks,
Audrey

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About