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

Re: [MacPerl] passing regexp to routine




On Mon, Jun 08, 1998 at 02:31:23PM -0400, John Deighan wrote:
} 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/);

Try it this way:

read_until('#EOF');

} 
} sub read_until { my($endpat) = @_;
} $line = <STDIN>;
} while ($line !~ $endpat) {

while ($line !~ /$endpat/) {

} 	print $line;
} 	$line = <STDIN>;
} 	}
} }
} 
} I guess the more basic question might be, can you assign a regular
} expression to a variable and later use it to do pattern matching?

Yes, but you need to make sure Perl recognizes it as a regular
expression.  // (m//) is an operator, and you can't put it in a string
and have it recognized as such without using an eval.

---
Paul Schinder
schinder@pobox.com


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