develooper Front page | perl.perl5.porters | Postings from June 2013

Re: [perl #117447] re modifier "h" - return named captures as hashexpression

Thread Previous | Thread Next
From:
Brian Gottreu
Date:
June 29, 2013 23:03
Subject:
Re: [perl #117447] re modifier "h" - return named captures as hashexpression
Message ID:
51CF67CF.5080204@gmail.com
 >>      my $fmt1 = '(?<y>\d\d\d\d)-(?<m>\d\d)-(?<d>\d\d)';
 >>      my $fmt2 = '(?<m>\d\d)/(?<d>\d\d)/(?<y>\d\d\d\d)';
 >>      my $fmt3 = '(?<d>\d\d)\.(?<m>\d\d)\.(?<y>\d\d\d\d)';
 >>
 >>      for my $d (qw(2006-10-21 15.01.2007 10/31/2005)) {
 >>          if (my (%date) = $d =~ m{$fmt1|$fmt2|$fmt3}h) {
 >>              while (my ($k,$v) = each %date) {
 >>                  print "$k = $v\n";
 >>              }
 >>          }
 >>      }

I kinda like the idea.

On 06/29/2013 02:36 PM, Tom Christiansen wrote:
 > And I don’t want us to keep adding /mods.  We have to think of
 > another way, something that embeds them and isn't stuck at mysterioius
 > one-letter identifiers.

Could you clarify what you mean by embed and what the antecedent of 
'them' is?

Since =~ binds a scalar expression to a pattern match, essentially 
replacing $_ with the expression, we could extend what =~ accepts on the 
left side.

($d, %date) =~ m{...} # %date = %+

and it would still return the same value (depending on context of course).

Following down that path, one could imagine

($d, $lvalue) =~ s{...}{...} # set /r implicitly

($d, @matches) =~ m{...} # @matches = ($1,$2,$3,...)

Or a functional interface could be used:

match(qr/.../, $d, named_captures => \%date)

I'm not necessarily advocating for any of these, it's just what I 
thought of.

Brian Gottreu

Thread Previous | 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