> >     $count = () = $string =~ /pattern/g;
> 
> Which I find cute as a demonstration of the Perl's context concept,
> but ugly as hell from usability viewpoint.  

I'd really like to see an RFC that looks into making the following
features more orthogonal:

        1. Return the number of matches

        2. Iterate over each match in sequence

        3. Return list of all matches

        4. Return a list of backreferences


Perl presently uses various combinations of /g and scalar/list context
to get these.  But some useful variants are missed.  For example,
suppose you have a string like this:

        "04-23-64 02-13-62 02-01-99 05-13-18 08-10-99"

You can run a loop once for each date:

        while ($string =~ /\d\d-\d\d-\d\d/g) {
          ...
        }

You can also extract the month-day-year parts of the first date:

        ($mo, $dy, $yr) =  ($string =~ /(\d\d)-(\d\d)-(\d\d)/);

But there is no convenient way to run the loop once for each date and
split the dates into pieces:

        # WRONG
        while (($mo, $dy, $yr) = ($string =~ /\d\d-\d\d-\d\d/g)) {
          ...
        }

This is an infinite loop.  It sets $mo $dy $yr to 04 23 64, repeatedly.

One solution here is:

        while ($string =~ /\d\d-\d\d-\d\d/g) {
          ($mo, $dy, $yr) = ($& =~ /(\d\d)-(\d\d)-(\d\d)/)
          ...
        }

Not only do you have to use $&, but you also have to write the pattern
twice.  

Another solution:

       @matches = ($string =~ /(\d\d)-(\d\d)-(\d\d)/g);
       while (@matches) {
         ($mo, $dy, $yr) = splice @matches, 0, 3;
         ...
       }

This is clumsy, and it doesn't work unless you know in advance how
many backreference groups the pattern will contain.  (Perl knows, and
this number is part of the struct regexp, but there is no way to get
Perl to tell you.)

My wish list for better orthogonality is actually a little longer than
the four items above, but the other items are more abstruse.

Reply via email to