Bug in perl interpretor: /pat/g iterator. 
Author Message
 Bug in perl interpretor: /pat/g iterator.

[This is a repost, the first posting had an error in my reply address]

    Hi everyone,

    I just encountered a strange behavior I can only explain as a bug
in the Perl interpretor. I'll just cut and paste some examples:

SunOS% perl # This works...
while ("012" =~ /\d/g) {print "-$`-$&-$'-\n";}
^D
--0-12-
-0-1-2-
-01-2--
SunOS% perl # This doesn't
while ("012" =~ /\d/ig) {print "-$`-$&-$'-\n";}
^D
<Lots of binary garbage was here>
SunOS%

    As you can see, the only difference between the working and the non-
working example is the presence of the "i" option in the pattern
matching operator.

    I tried this test on two different systems, a Sun and a SGI machine,
and got the same result on both. Executing "perl -v" returns

Quote:
> This is perl, version 4.0

> $RCSfile: perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $
> Patch level: 36

    I hope this is not old news. Would somebody test these constructs
on other platforms and tell me what happens? What about Perl5?

    I wonder how I am going to fix my script... /g iteration is so
useful! I wonder why simply turning on the case-insensitive flag
of the pattern match routines would cause such a problem?

    Pierre
--
=============================================================================


=========================================================== Achille Talon ===
--
=============================================================================


=========================================================== Achille Talon ===



Sat, 12 Oct 1996 05:23:43 GMT  
 Bug in perl interpretor: /pat/g iterator.
:     I just encountered a strange behavior I can only explain as a bug
: in the Perl interpretor. I'll just cut and paste some examples:
:
: SunOS% perl # This works...
: while ("012" =~ /\d/g) {print "-$`-$&-$'-\n";}
: ^D
: --0-12-
: -0-1-2-
: -01-2--
: SunOS% perl # This doesn't
: while ("012" =~ /\d/ig) {print "-$`-$&-$'-\n";}
: ^D
: <Lots of binary garbage was here>
: SunOS%
:
:     As you can see, the only difference between the working and the non-
: working example is the presence of the "i" option in the pattern
: matching operator.
:
:     I hope this is not old news. Would somebody test these constructs
: on other platforms and tell me what happens? What about Perl5?

It's a bug in Perl 4.  It's already fixed in Perl 5, though you're
gonna get a spurious error in Alpha 8 from trying to match against
a constant string.  (I just fixed the spurious error.)  You can
work around that by putting "012" into a variable.

:     I wonder how I am going to fix my script... /g iteration is so
: useful! I wonder why simply turning on the case-insensitive flag
: of the pattern match routines would cause such a problem?

Here's the semi-official patch for Perl 4 if you're so inclined.

*** regexec.c.old       1992/06/08 15:25:50
--- regexec.c   1994/03/08 22:20:51
***************
*** 449,458 ****
        prog->subend = strend;
        if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
                strend += dontbother;   /* uncheat */
!               if (safebase)                   /* no need for $digit later */
                    s = strbeg;
                else if (strbeg != prog->subbase) {
-                   i = strend - string + (stringarg - strbeg);
                    s = nsavestr(strbeg,i);     /* so $digit will work later */
                    if (prog->subbase)
                            Safefree(prog->subbase);
--- 449,460 ----
        prog->subend = strend;
        if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
                strend += dontbother;   /* uncheat */
!               i = strend - string + (stringarg - strbeg);
!               if (safebase) {                 /* no need for $digit later */
                    s = strbeg;
+                   prog->subend = s+i;
+               }
                else if (strbeg != prog->subbase) {
                    s = nsavestr(strbeg,i);     /* so $digit will work later */
                    if (prog->subbase)
                            Safefree(prog->subbase);
***************
*** 460,466 ****
                    prog->subend = s+i;
                }
                else {
-                   i = strend - string + (stringarg - strbeg);
                    prog->subbeg = s = prog->subbase;
                    prog->subend = s+i;
                }
--- 462,467 ----

Larry



Sun, 13 Oct 1996 09:52:29 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Bug with /pat/g iterator?

2. Compile RE pat only when I change pat?

3. bug in /$pat/io ?

4. Bug with s/$pat/$sub/ (or not?)

5. how to run Perl script on remote server not having Perl interpretor

6. Win95 Perl interpretor

7. Perl Interpretors

8. tcl/tk interpretor in java

9. tcl/tk interpretor in java

10. My first OO perl script: array plus iterator?

11. New Perl module: Iterator.pm

12. New Perl module: Iterator.pm

 

 
Powered by phpBB® Forum Software