Dear monks,

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

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.