[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] passing regexp to routine



Carl Johan Berglund writes:
|You can assign a string to a variable and use it in a regex:
|read_until('#EOF');
|sub read_until {
|   my $regex = shift;
|   while (<>) {
|      last if /$regex/;
|      # Do whatever you want to do
|   }
|}
|The problem with this is that the string has to be compiled into an
|internal regex structure - I guess there is a faster way to do it.

Yes: last if /$regex/o;

The trailing o tells perl to compile the expression once instead of every
time through the loop. Being *every* expression has to be compiled at least
once, /$regex/o isn't any slower than /hard coded regex/.

Brian

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch