grasbueschel has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Avoid eval() / dynamic regular expressions
by BrowserUk (Patriarch) on Dec 15, 2009 at 02:20 UTC | |
|
Re: Avoid eval() / dynamic regular expressions
by ikegami (Patriarch) on Dec 15, 2009 at 01:06 UTC | |
by grasbueschel (Initiate) on Dec 15, 2009 at 08:43 UTC | |
|
Re: Avoid eval() / dynamic regular expressions
by crashtest (Curate) on Dec 15, 2009 at 01:49 UTC | |
by grasbueschel (Initiate) on Dec 15, 2009 at 08:48 UTC | |
|
Re: Avoid eval() / dynamic regular expressions
by Logicus (Initiate) on Aug 19, 2011 at 13:34 UTC |