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?


In reply to Global %+ woes by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.