I think hash assignment within regex's would be more useful than 
variable assignment (though there's no reason there couldn't be both, 
I suppose).  Here's a copy of something I sent to p5p a while back:


I suggest that (?%field_name: pattern) spit out 'field_name', in 
addition to the matched pattern, when matching in a list context:

     $text = "abajace -- mailbox full";
        %hash = $text =~ /^ (?% username : \S+) \s*--\s* (?% reason : .*)$/xsi;

would result in %hash = (username => 'abajace', reason => 'mailbox full').


Why This Would be Useful:

Often one wants to match a string against a list of patterns which 
extract similar information from the string, but in varying order. 
Also, some fields might get extracted by some patterns and not by 
others.  Continuing with the (over-simplified) example of analyzing 
e-mail bounce messages:

   my @regexps = (

       # 'abajace -- mailbox full' or 'abajace -- user unknown'
       q/^ \s* (?% username  : \S+) \s*--\s* (?% reason : .*)$/,
 
       # 'Unknown local part: flycrake'
       q/^ \s* (?% reason : Unknown\ local\ part): \s* (?% username  : \S+)/,
 
       # 'New address for abajace is [EMAIL PROTECTED]'
       q/(?% reason : new\ address\ for) \s+ (?% username  : \S+) \s+ is \s+
                (?% new_address : \S+\@\S+)/,

   );

   while (my $bounce_text = get_next_message()) {
       my %field = ();
       for my $regexp (@regexps) {
           if ( %field = $bounce_text =~ /$regexp/xsi;) {
               print "username: $field{username}, reason: $field{reason}\n";
               if ($field{new_address}) {
                   change_address($field{username}, $field{new_address});
               }
               last;
           }
       }
   }

I don't have any strong attachment to the above suggested syntax -- 
I'd just like to have the functionality.

Reply via email to