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

Re: [MacPerl] passing regexp to routine



Hi!

On Mon, 8 Jun 1998, 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/);
> 
> sub read_until { my($endpat) = @_;
> $line = <STDIN>;
> 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?

Why not

--- snip ---
#!/usr/bin/perl

read_until('#EOF');

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

OK, so you get lts (leaning toothpick syndrome) by hard-coding the
slashes, 

NO YOU DONT! I just tested it. Man, is perl smart or what. When you have a
variable inside the pattern it "understands" that stuff inside the
variable doesn't stop the pattern (This is perl, version 5.004_04 built
for alpha-linux). And if I really cared, I'd probably even be able to find
that documented... 

Remember: a pattern is not a string. It's neighter an rvalue nor an
lvalue(or so I think) -- you cannot assign from it, and you can't assign
to it.

Easy on the replies, this is not Mac-specific.

-Johan
--
Johan Almqvist                          johana@netch.se
Netch Technologies AB                   046-270 16 00 (vx)
Programmerare                           070-697 699 8
                                        ICQ# 11416872


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