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

Re: [MacPerl] passing regexp to routine



At 18.36 +0200 98-06-09, Brian "L." Matthews wrote:
>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/.

That's OK if you always want to call the subroutine once, but if you call
the same subroutine again with another $regex string, perl will continue
using the old compiled regex, because of the /o. It means once, not once
per subroutine call.

To really recompile the regex only when calling the subroutine, we could
use the fact that the null pattern ($var =~ //;) defaults to the last
successfully executed regex. We could use:

read_until('#EOF');

sub handle {
	my $line = shift;
	# Do whatever you want to do with the lines
	print $line;
}

sub read_until {
	my $regex = '^(?!'.shift().')';	# Negate the regex,
	my $line = <DATA>;		# since
	$line =~ /$regex/ or return;	# this match has to be successful
	handle $line;
	while (<DATA>) {
		last unless //;		# Here perl will use the above regex
		handle $_;
	}
}

__END__
Blah
Blah
Blah
#EOF
Blah
Blah

___Carl_Johan_Berglund_________________________
   Adverb Information
   carl.johan.berglund@adverb.se
   http://www.adverb.se/



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