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

Re: Bug report: split splits on wrong pattern

Thread Previous | Thread Next
From:
Radu Greab
Date:
June 27, 2001 11:51
Subject:
Re: Bug report: split splits on wrong pattern
Message ID:
15162.11020.279064.471031@ix.netsoft.ro
On Wed, 27 Jun 2001 13:33 +0200, Marcel Grunauer wrote:
 > I've spent the last hour trying to find out why the third call to
 > mysplit() in the program below produces the wrong result, but the
 > first one produces the correct one. It seems once you call mysplit()
 > with the default pattern of /\s+/, this pattern is used all the
 > time from then on.

 > ---------------8<---------------
 > #!/usr/bin/perl
 > 
 > use warnings;
 > use strict;
 > 
 > sub mysplit {
 >          my ($orig, $pat, $limit) = @_;
 >          $pat = qr/\s+/ unless ref($pat) eq 'Regexp';
 >          $limit ||= 0;
 >          print "orig  = <$orig>\n";
 >          print "pat   = <$pat>\n";
 >          print "limit = <$limit>\n";
 >          $DB::single = 1;
 >          print join ':' => split($pat, $orig, $limit);
 >          print "\n\n";
 > }
 > 
 > my $t = 'hello cruel world';
 > mysplit($t, qr/ll/);
 > mysplit($t);
 > mysplit($t, qr/ll/);
 > ---------------8<---------------
 > 
 > Output:
 > 
 > ---------------8<---------------
 > orig  = <hello cruel world>
 > pat   = <(?-xism:ll)>
 > limit = <0>
 > he:o cruel world
 > 
 > orig  = <hello cruel world>
 > pat   = <(?-xism:\s+)>
 > limit = <0>
 > hello:cruel:world
 > 
 > orig  = <hello cruel world>
 > pat   = <(?-xism:ll)>
 > limit = <0>
 > hello:cruel:world
 > ---------------8<---------------


The patch below against perl@10966 fixes it. If PMf_WHITE was set once
in pp_regcomp(), then it remained active for the next regular
expressions using the same PMOP.


Thanks,
Radu Greab


--- pp_ctl.c~	Tue Jun 26 16:29:51 2001
+++ pp_ctl.c	Wed Jun 27 21:14:35 2001
@@ -147,8 +147,11 @@
 
     if (!PM_GETRE(pm)->prelen && PL_curpm)
 	pm = PL_curpm;
-    else if (strEQ("\\s+", PM_GETRE(pm)->precomp))
-	pm->op_pmflags |= PMf_WHITE;
+    else
+	if (strEQ("\\s+", PM_GETRE(pm)->precomp))
+	    pm->op_pmflags |= PMf_WHITE;
+	else
+	    pm->op_pmflags &= ~PMf_WHITE;
 
     /* XXX runtime compiled output needs to move to the pad */
     if (pm->op_pmflags & PMf_KEEP) {
--- t/op/split.t~	Tue Jun 26 16:30:17 2001
+++ t/op/split.t	Wed Jun 27 21:42:53 2001
@@ -5,7 +5,7 @@
     @INC = '../lib';
 }
 
-print "1..44\n";
+print "1..45\n";
 
 $FS = ':';
 
@@ -244,3 +244,13 @@
     print "ok 44\n";
 }
 
+{
+    # check that PMf_WHITE is cleared after \s+ is used
+    # reported in <20010627113312.RWGY6087.viemta06@localhost>
+    my $r;
+    foreach my $pat ( qr/\s+/, qr/ll/ ) {
+	$r = join ':' => split($pat, "hello cruel world");
+    }
+    print "not " unless $r eq "he:o cruel world";
+    print "ok 45\n";
+}

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