Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!udel!haven!uvaarpa!mmdf From: worley@compass.com (Dale Worley) Newsgroups: comp.lang.perl Subject: Sluggish patterns. Message-ID: <1991Apr1.181801.16850@uvaarpa.Virginia.EDU> Date: 1 Apr 91 18:18:01 GMT Sender: mmdf@uvaarpa.Virginia.EDU (Uvaarpa Mail System) Reply-To: worley@compass.com Organization: The Internet Lines: 44 X-Name: Felix Lee I have a pattern /$cookie/ that appears in an inner loop and gets evaluated thousands of times. Recompiling the pattern every time is terribly inefficient, since $cookie almost never changes. Well, here's the original loop: while (! $done) { &begin(); &eat() if /$cookie/; &run() if /$monster/; &end(); } The usual solution runs along the lines: $cookie = ...; $monster = ...; eval "sub cookie { &eat() if /$cookie/o; }"; eval "sub monster { &run() if /$monster/o; }"; while (! $done) { &begin(); &cookie; &monster; &end(); } Now, anytime $cookie is changed, reexecute the first eval, and anytime $monster is changed, reexecute the second eval. If there was only one pattern searched for in the loop, you could exploit the fact that // reuses the previous pattern. Dale Dale Worley Compass, Inc. worley@compass.com -- Heard on "The Daily Feed", a syndicated political satire radio program: Saddam Hussein is not Hitler; if he were, France would have surrendered by now.