This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1  TITLE

Assignment within a regex

=head1 VERSION

        Maintainer: Richard Proctor <[EMAIL PROTECTED]>
        Date: 16 Aug 2000
        Version: 1
        Mailing List: [EMAIL PROTECTED]
        Number: 112

=head1 ABSTRACT

Provide a simple way of naming and picking out information from a regex
without having to count the brackets.

=head1 DESCRIPTION

If a regex is complex, counting the bracketed sub-expressions to find the
ones you wish to pick out can be messy.  It is also prone to maintainability
problems if and when you wish to add to the expression.  Using (?:) can be
used to surpress picking up brackets, it helps, but it still gets "complex".  
I would sometimes rather just pickout the bits I want within the regex itself.

Suggested syntax: (?$foo= ... ) would assign the string that is matched by
the patten ... to $foo when the patten matches.  These assignments would be
made left to right after the match has succeded but before processing a 
replacement or other results.  There may be whitespace between the $foo and
the "=".  This would not give the backrefs \1 etc that come with conventional
bracketed sub expressions, I don't think this would be a problem.
Potentially the $foo could be any scalar LHS, as in (?$foo{$bar}= ... )!,
likewise the '=' could be any asignment operator.

The camel and the docs include this example:

   if (/Time: (..):(..):(..)/) {
        $hours = $1;
        $minutes = $2;
        $seconds = $3;
    }
        
This then becomes:
 
  /Time: (?$hours=..):(?$minutes=..):(?$seconds=..)/

This is more maintainable than counting the brackets and easier to understand
for a complex regex.  And one does not have to worry about the scope of $1 etc.

(Note I am only on the announce list at present as I am suffering
from negative free time).

=head1 IMPLENTATION

No idea

=head1 REFERENCES

I brought this up on p5p a couple of years ago, but it was lost in the noise...



Reply via email to