in reply to Re^2: RFC: named pattern match tokens
in thread RFC: named pattern match tokens

I thought the same. I think a nice name of the hash would be %~. =~ is matching so why couldn't $~{name} be a named match. Here is the code I ended up with:

... sub convert { my $re = shift; $re =~ s( \\ ( \\ | C\{ (?>\s*) ((?>\w+)) (?>\s*) \} ) ) { defined $2 ? "(?{\$~{$2}=\$^N})" : "\\" }xeg; "(?{undef(%~)})" # clear the %~ .$re ."(?{\$~{\$_}=\${\$_} for(1..\$#+)})"; # add the numbered matches } ... my $re = qr/(\w+)\C{baz}(?: - (\w+)\C{qux})?(\+\d+)/; "three - four - five+89" =~ $re; print "baz=$~{baz}, qux=$~{qux}, $~{3}\n";
Please note that even the named matches got their number! Maybe they should not, I think I could implement that if I needed.

I also considered syntax like this:

my $re = qr/(?\$bar=\w+) - (?\$qux{not}=\w+)/;
which could naively be implemented like this:
... sub convert { my $re = shift; $re =~ s<\(\?\\\$([^=]+)=([^)]*)\)><($2)(?{\$$1=\$^N})>g; $re } ...
but the problem is that I don't know how to make sure you can do even things like :
my $re = qr/...(?\$var=a(\d+|\w-\w+)b).../;
I don't know how to find the right closing bracket.

Jenda
We'd like to help you learn to help yourself
Look around you, all you see are sympathetic eyes
Stroll around the grounds until you feel at home
   -- P. Simon in Mrs. Robinson

Replies are listed 'Best First'.
Re^4: RFC: named pattern match tokens
by diotalevi (Canon) on Oct 04, 2004 at 23:10 UTC
    • %~ is not available for your use. Punctuation variables are reserved for perl's use. The ^_ namespace is reserved for this use. The closest available analogue of %~ is %{'^_~'} because %^_~ is a syntax error. I'd suggest %^_C to follow the \C{name} theme.

    • Hashes are cleared by assigning an empty list, not by undefining them. When you say %hash = () you allow perl to be smart about the allocation of the memory associated with %hash. undef %hash circumvents this and forces some unnecessary work.

    • I deliberately placed the new syntax to the right of the capture because otherwise I would have had to do some balanced delimiter matching. perlop covers the requirements for matching (...) in regexps. It is possible, I just couldn't do it in the two minutes it took to write the initial example.

      The implication of allowing $~{EXPR} to inform the creation of the hash key is that you must allow arbitrary perl code inside EXPR. This is not a problem if you take into account the same balanced-tag handling already mentioned in perlop.

      To do this really well requires Text::Balanced and an understanding of Gory details of parsing quoted constructs from perlop.