sushant.ravale has asked for the wisdom of the Perl Monks concerning the following question:

I get first input from user which is a tree (having significant height and depth) of nodes. Each of the node contains a regex and modifiers. This tree gets saved in memory. This is taken only once at the application startup.

The second input is a value which is matched starting at the root node of the tree till an exact matching leaf node is found (Depth First Search). The match is determined as follows :

my $evalstr = <<EOEVAL; if(\$input_value =~ /\$node_regex/$node_modifiers){ 1; }else{ -1; } EOEVAL no strict 'refs'; my $return_value = eval "no strict;$evalstr";

The second input is provided continuously throughout the application's life time by a source.

problem: The above code works very well for some time (approx. 10 hours), but after continuous input for this time, the eval continuously starts failing and I get -1 in $return_value. All other features of the application work very fine including other comparison statements.If I restart the application, the matching again starts and gives proper results.

Observations: 1) I get deep recursion warning many times, but I read somewhere it is normal as stack size for me would be more than 100 many a times, considering the size of the input tree. 2) If I use simple logic for regex match without eval as above, I don't get any issue for any continuous run of the application.

if($input_value =~ /$node_regex/){ $return_value=1; }else{ $return_value=-1; }
but then I have to sacrifice dynamic modifiers, as per Dynamic Modifiers

Checks: 1) I checked $@ but it is empty. 2) Also printed the respective values of $input_value,$node_regex and $node_modifiers, they are correct and should have matched the value with regex at the failure point. 3) I checked for memory usage, but it's fairly constant over the time for the perl process. 4) Was using perl 5.8.8 then updated it to 5.12, but still face the same issue.

Question : What could be the cause of above issue? Why it fails after some time, but works well when the application is restarted? I need to support following modifiers: gismox, Can I support the modifier g inside the pattern itself? I read somewhere that I can't do that as g affects the way the regex is used rather than the regex itself. update:The regex failed to match was : ^(0|1)$ , most of the regex input were simple strings, ^0-5$ and ^(0|1)$ being the most complex! All the regex input were valid for 10 hours and suddenly they started failing to match.

Replies are listed 'Best First'.
Re: eval failing to match regex after sometime
by Kc12349 (Monk) on Oct 05, 2011 at 17:52 UTC

    One thought I have is that there may potentially be a memory issue. How often is the /g modifier used? This modifier will remember position and in some cases this may lead to a growing memory foot print.

    I might try running the script without including /g to try to rule that out.

    Beyond this, an individual regex could cause an issue if you are not validating them in any way. In addition to global matching something like a recursive regex could create an infinite loop.

    What type of regex patterns are potentially being passed in? If you can find a way of seeing what patterns were processed just before the problem begins this would likely be helpful.

    If all else fails, you could also look at unrolling your recursion. This thread is a good starting point should you venture down that road. Turning a recursive function into an iterator

    After some aggravation in writing recursive node structure processors, I now in practice write them using a stack algorithm instead. I believe the above thread likely has an example of such an approach.

Re: eval failing to match regex after sometime
by Anonymous Monk on Oct 05, 2011 at 14:32 UTC