develooper Front page | perl.perl5.porters | Postings from June 2008

Re: Empty regex

Thread Previous | Thread Next
From:
Yitzchak Scott-Thoennes
Date:
June 18, 2008 22:54
Subject:
Re: Empty regex
Message ID:
47489.63.231.49.141.1213854868.squirrel@webmail.efn.org
On Wed, June 18, 2008 9:54 pm, alfie@share-house.com.au wrote:
> Can somebody show me when current behavior of /$empty/ would *ever* be a
> good idea? I just don't get it.

Without meaning to be pedantic, it's a good idea when you have a variable
that may have a pattern to match or may be empty to indicate that the
last successful pattern should be reused.  The reuse-last-pattern use
case doesn't occur very often, and then almost exclusively in parsing
code, but it does occur.

I was going to write something more about how probably no one would
argue for adding such a feature now, but given that the feature exists
and has existed for so long, warning about it would be a bad thing.

...but while starting to do so, I came around to the other side.  It's
possible to replace /$empty/ with length($empty)?/$empty/://, so I'd
advocate going ahead and deprecating the current behavior of /$empty/,
since it can cause so much pain if you don't know of the feature.

WRT split, this is *not* an exception in split, nor is it actually a regex
feature.  Rather, it is a feature of the match and substitute operations,
but not split or qr.  And there's a bug here, triggered by an
optimization:

$ perl -w
use Test::More 'no_plan';
my $x = qr//;
ok("foo" =~ /foo/ && "bar" =~ /$x$x/);
ok("foo" =~ /foo/ && "bar" =~ /$x/);
ok 1
not ok 2



Thread Previous | Thread Next


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