develooper Front page | perl.perl5.porters | Postings from December 2006

Re: [perl #41115] Empty regular expression

From:
demerphq
Date:
December 22, 2006 04:05
Subject:
Re: [perl #41115] Empty regular expression
Message ID:
9b18b3110612220405pc7cd49dseb2d9f17fd36f70c@mail.gmail.com
On 12/22/06, Michael G Schwern <schwern@gmail.com> wrote:
> demerphq wrote:
> > If you truely wish to match nothing use the pattern /(?:)/
>
> Or just remove the match entirely since that will match anything.


Well yeah. The reason I mentioned (?:) was that I assumed that this
issue came from something like:

  if ($foo=~/$bar/) {  .... }

which suffers from the problem that if $bar is empty it wont do what
most people expect. Consider:

perl -le"$_='foo'; /(foo)/; $x=''; print qq($1) if /$x/;"

will output: 'foo'

The way around it is to change the pattern to

  if ($foo=~/(?:)$bar/) {  .... }

or to

  if ($foo=~/(?#)$bar/) {  .... }

Personally I dont like the behaviour of the empty pattern and would
just as soon see it deprecated and removed.

Yves


-- 
perl -Mre=debug -e "/just|another|perl|hacker/"



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About