in reply to better way to get last named capture group

If your goal was to mimic the exact Python semantic, I'd try to patch or subclass Tie::Hash::NamedCapture to record the last name set.

> Internally %+ and %- are implemented with a real tied interface via Tie::Hash::NamedCapture

In a hurry I couldn't find the implementation, not sure if it was done in pure Perl or XS/C.

But even if in C, monkey patching STORE with a wrapper should do the job

update

Never mind.

I ran some experiments and couldn't succeed in patching STORE to any effect.

And according to the docs

> %- and %+ are tied views into a common internal hash associated with the last successful regular expression.

this wouldn't help either, because the internal hash is out of reach ... too bad.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

  • Comment on Re: better way to get last named capture group

Replies are listed 'Best First'.
Re^2: better way to get last named capture group
by ysth (Canon) on Jul 03, 2025 at 02:25 UTC
    The source is in perl's universal.c, but the update tie methods don't do anything; the read tie methods call named_buff or named_buff_iter to get the data from the regex engine. So no patching possible.
      Yeah, the tie is basically only a frontend to the regex engine.

      One side effect is that you can tie other %hashes to act like %+ and %-

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

Re^2: better way to get last named capture group
by ikegami (Patriarch) on Jul 03, 2025 at 10:56 UTC

    It's in universal.c.

    It would be possible to modify Perl to provide something like

    tied( %+ )->latest_capture_name