I'm trying to speed up my little perl script by finding a way to avoid eval() or use it more efficiently. What the script does, is to process csv files and fill out two fields at the end of each line using regular expressions. There're a lot of these expressions (currently ~200) and they should be editable outside of the perl code. In the end, some fields of these csv's will be part of an SQL INSERT INTO statement.
So what I have is this:
# $filter holds regex statements # e.g. s/(F01-861385.*);;/\1;Categroy1;SubCategory2/g open(REGEX, "<$filter"); while(<REGEX>) { next if ($_ =~ m/^\s*#.*$/); chomp; $regex .= "$_, "; } close(REGEX); # pre formatted csv content in $lines for (split /^/, $lines) { chomp; $_ =~ eval($regex); # ... # extract fields and build up SQL INSERT INTO }
This is slow.... :)
Now I'm trying to find a way to improve things: Is there a way to compile the eval only once? Or do I need more out-of-the-box thinking and there's the possibility to achieve the same goal (having users setting up their own regex and include them dynamically in the code) more efficiently?
In reply to Avoid eval() / dynamic regular expressions by grasbueschel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |