Dear fellow Monks,

May I once more lay before you the question of communication between a Perl program and external regexfiles. My objective is to find a mechanism, with as loose a coupling as possible, that makes it possible for a Perl program to 'query' a regex for its captured values.

The regex can have a prefix codeblock that inits some local variables, that can be populated with the captured values of the regex, and then, in a postfixed codeblock, stored in a hash. This results hash must then be made accessible to the container program.

I have this working, but only by using a hash variable that the container must know about and manipulate. And although I think it can be put to good use as is, I am looking for a way where the container program needs no prior knowledge, and just 'queries', somehow. Maybe there are perl globals that I overlooked?

Here is an example:

(?{ # prefix codeblock: local $number = ""; local $name = ""; local $rest = ""; }) ^ [\ \t]* (?: ([0-9]+)\.[ ] (?{ $number = $^N; }) ) (?: ([A-Z][A-Z]+)[ ]* (?{ $name = ucfirst(lc($^N)); }) ) (?: (.*) (?{ $rest = $^N; }) ) $ (?{ # postfix codeblock: ${$hashname}{ 'number' } = $number; ${$hashname}{ 'name' } = $name; ${$hashname}{ 'rest' } = $rest; })

The variable $hashname is translated by the container program to the proper variable name, with namespace. This is simply search and replace to prevent real hardcoding in the regexfile.

Any comment on the problem and my tentative solution is very welcome. Is there a better way?

update layout and typos, shortened a little


In reply to Communication of program(s) with regex codeblocks by erix

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.