John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Here is some prototype work concerning using regex to parse, meaning "which of several possible patterns do I encounter first?"

method do_format (Str $line) { ##experiment with parsing
my $simples = qr{\*\*}; # extensible by user. Same opening and clos +ing. my $ps= qr{(?<prematch>.*?) (?: (?:(?<!http:)//) \s* (?<body>.*?)\s*(?: (?:(?<!http:)//)|\Z)(*:i +talic) # blah blah | (?<simple>$simples)\s*(?<body>.*?)\s*(?:\k<simple>|\Z)(*:simpl +e) | \\\\ (*:break) | \[\[\s*(?<body>.*?)\s*\]\](*:link) | \Z (*:nada) # must be the last branch ) }xs; my @results; while ($line =~ /$ps/g) { # careful not to trash my capture variables! So no using regex at + all until I determined what I need and saved it. my $prematch= $+{prematch}; my $s; my $body= $+{body}; if ($REGMARK eq 'simple') { my $style= $+{simple}; $s= $self->simple_format ($style, $body); } elsif ($REGMARK eq 'italic') { $s= $self->simple_format ('//', $body); } elsif ($REGMARK eq 'break') { $s= $self->format_tag ($self->get_tag_data('br'), undef); } elsif ($REGMARK eq 'link') { $s= $self->process_link_body ($body); } # other cases... push @results, $self->escape($prematch) unless length($prematch)= +=0; push @results, $s if defined $s; } return join ('', @results); }
I'm using the (*MARK:NAME) / $REGMARK feature to determine which branch was taken, which looks like a promising approach: no nasty code blocks needed! No inefficient probes to see which named captures were actually set, either.

However, I then examine different named captures (different names and different number of them) depending on the branch. Ideally I'd just refer to the ones I needed, in the matching branch of the code that deals with that case. But, I can't refer to any kind of regular expression usage until I'm completely done using %+, and that includes (I presume) using given/when as well as other functions in the object such as $self->escape.

I'm finding, as you can see in the little bit above, that this is causing the code to be messy and/or inefficient. It would be cleaner to take care of escape($prematch) at the beginning before the switch statement, for example.

I'm thinking that before I use this for a full-sized problem, I want to get out of this restriction. I want to be able to call any methods or use any language features I want, not convolute the code!

So, do I save every possible named capture locally immediately after the regex? That could be a long list and most of them won't be needed. Can I copy all the keys found in the funny tied %+, and although compact to write, would that be slow?

I read a mention in passing that %+ and %- are both tied views to the same underlying object. So, can I just grab that object and swap it with a blank one, so now it's mine to keep and a subsequent regex won't clobber it? I'm hoping I can, and furthermore feel that how to do this (or the introduction of improvements to allow it) should be promoted as a way to localize those variables.

Anybody have any clues, or know how it works in-depth?

Replies are listed 'Best First'.
Re: Global %+ woes
by JavaFan (Canon) on Apr 26, 2011 at 13:41 UTC
    Can I copy all the keys found in the funny tied %+
    Yes: my %whatever = %+;
    would that be slow?
    Slow is a relative term. Did you try? Did you benchmark it? Do you really think the speed of copying %+ matters when compared to running the regexp engine?
    I read a mention in passing that %+ and %- are both tied views to the same underlying object. So, can I just grab that object and swap it with a blank one, so now it's mine to keep and a subsequent regex won't clobber it?
    Have you looked at Tie::Hash::NamedCapture?
      Have you looked at Tie::Hash::NamedCapture?
      Yes, I have. It's a stub with one trivial function. The magic lives in XS and in the Regex core somewhere.