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

Re: [MacPerl] passing regexp to routine



>I would like to write a routine (function, whatever) that reads input until
>it encounters a line that matches a particular pattern. To do that, I'd
>like to pass a regular expression to a routine. Is that possible? I tried
>it in the obvious way, and it didn't work:
>
>read_until(/#EOF/);
>
>sub read_until { my($endpat) = @_;
>$line = <STDIN>;
>while ($line !~ $endpat) {
>	print $line;
>	$line = <STDIN>;
>	}
>}
>

Muaha! My semi-annual contribution to the list (I hope I didn't
miss my last one :-):

===== perl here =====
sub readAndPrintTillMatch
{
	my ($theFileHandle,$theMatch,$theResponse) = @_;
	my $output;
	do {
		$output = <$theFileHandle>;
		print "$output";
		flush(STDOUT);
	} until ($output =~ /$theMatch/);
	print $theFileHandle "$theResponse\n";
	print "$theResponse\n";
	flush(STDOUT);
}

	&readAndPrintTillMatch(SOCKET,"Please enter your term type","01");
===== perl here =====

You pass in a file handle, pattern and response, and it will read
from the file handle (writing to STDOUT what it reads) until it
finds a match, then writes the response to the file handle, prints
the response to STDOUT and returns.  I use this for a mud robot
when creating new mud characters, passing in a socket :-).  Strip
out anything you don't need, and there you go!

Tom.
Tom Kimpton
--
Mrs. Bun: Have you got anything without spam?
Waitress: Well, there's spam egg sausage and spam, that's not got
much spam in it.
Mrs. Bun: I don't want any spam!

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