Re: in the elemetns of programming perl , I need help with 2 lines that I don't understand

[email protected] ("Chas. Owens")
Newsgroups perl.beginners
Message-ID <[email protected]>
On Sun, Jun 1, 2008 at 3:39 PM, Rob Dixon <[email protected]> wrote:
> Dr.Ruud wrote:
>> Richard Lee schreef:
>>
>>> $pattern = '.*(?:' . join('|', @ARGV) . ')';
>>
>> Safer:
>>
>>   $pattern = '.*(?:' . join('|', map quotemeta, @ARGV) . ')';
>>
>> And I would do a qr() on top of that.
>
> How? And why?
snip

how:

my $pattern = '.*(?:' . join('|', map quotemeta, @ARGV) . ')';
$pattern = qr/$pattern/;

why:

To compile the regex.  In the original program, the regexes use the o
modifier to promise that $pattern won't change so the optimizer can
compile the regexes once, but if you use qr// then you don't have to
make that promise and you still only compile the regex once.



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.