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

Re: [perl #41115] Empty regular expression

From:
demerphq
Date:
December 20, 2006 13:47
Subject:
Re: [perl #41115] Empty regular expression
Message ID:
9b18b3110612201347r1a7e5d8aj549fb67f3448cebb@mail.gmail.com
On 12/20/06, via RT aml @ rulezz. ru <perlbug-followup@perl.org> wrote:
> # New Ticket Created by  aml@rulezz.ru
> # Please include the string:  [perl #41115]
> # in the subject line of all future correspondence about this issue.
> # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=41115 >
>
>
>
> This is a bug report for perl from aml@rulezz.ru,
> generated with the help of perlbug 1.35 running under perl v5.8.4.
>
>
> -----------------------------------------------------------------
> [Please enter your report here]
>
> Empty regular expression s/...// has a strange behaviour.
> Its result depends on previously evaluated regular expression.
> Look at the minimal proof of concept:
>
> #!/usr/bin/perl
>
> {
>         print 'Correct result: ';
>
>         &test;
> }
>
> {
>         print 'Correct result: ';
>
>         my $a = 'c';
>         $a =~ s/c//;
>
>         &test;
> }
>
> {
>         print 'Incorrect result: ';
>
>         my $a = 'd';
>         $a =~ s/d//;
>
>         &test;
> }
>
> sub test
> {
>         print(('abc' =~ //) ? 'true' : 'false');
>         print "\n";
> }
>
> Subroutine 'test' contains this strange regexp: sometimes it returns true, and sometimes false.

Hi,

This is actually documented behaviour. The empty pattern when used in
a m// or s/// match operator is special cased to mean the pattern used
in the last successful match.

If you truely wish to match nothing use the pattern /(?:)/

I will close this ticket as not-a-bug. Thanks for the report anyway.

Cheers,
Yves
ps: Yes this can be a trap that probably shouldn't have been
introduced, but its much too late to do anything about it now.
-- 
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