erix has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Communication of program(s) with regex codeblocks
by ikegami (Patriarch) on Oct 26, 2004 at 14:54 UTC | |
by erix (Prior) on Oct 26, 2004 at 15:46 UTC | |
by ikegami (Patriarch) on Oct 26, 2004 at 16:22 UTC | |
by erix (Prior) on Oct 28, 2004 at 21:26 UTC | |
by ikegami (Patriarch) on Oct 28, 2004 at 22:33 UTC | |
|