develooper Front page | perl.perl5.porters | Postings from April 2003

[perl #21999] Unsuccessful regex still alters $1

From:
Peter Scott
Date:
April 21, 2003 08:10
Subject:
[perl #21999] Unsuccessful regex still alters $1
Message ID:
rt-21999-55397.18.360166107113@bugs6.perl.org
# New Ticket Created by  Peter Scott 
# Please include the string:  [perl #21999]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=21999 >


I was raised on the belief that "an unsuccessful regex match leaves $1 and 
friends at their previous values." So I was surprised to discover an 
exception to that rule.  It's been around for a long time, so perhaps I'm 
missing something.  OTOH, if this is not expected behavior, perhaps there's 
a bug.

% cat foo
#!/usr/bin/perl
for (qw(843 foo 23skidoo bar 345)) {
   print /^(\d+)$/ ? "$_ matched" : "$_ didn't match";
   print ";  \$1 = $1\n";
}

% ./foo
843 matched;  $1 = 843
foo didn't match;  $1 = 843
23skidoo didn't match;  $1 = 8
bar didn't match;  $1 = 8
345 matched;  $1 = 345

In the case of the unsuccessful match against 23skidoo, $1 is truncated to 
the first character of its previous value. This is in 5.6.1, 5.8.0, and 5.9.0.

Just as a historical point of reference: In 5.004_04, $1 is set to the 
first character of 23skidoo, and is undef on an unsuccessful match:

843 matched;  $1 = 843
foo didn't match;  $1 =
23skidoo didn't match;  $1 = 2
bar didn't match;  $1 =
345 matched;  $1 = 345


Summary of my perl5 (revision 5.0 version 9 subversion 0 patch 19077) 
configuration:
   Platform:
     osname=linux, osvers=2.4.18-3, archname=i586-linux
     uname='linux psdt.com 2.4.18-3 #1 thu apr 18 07:31:07 edt 2002 i586 
unknown '
     config_args='-des -Dusedevel'
     hint=recommended, useposix=true, d_sigaction=define
     usethreads=undef useithreads=undef usemultiplicity=undef
     useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
     use64bitint=undef use64bitall=undef uselongdouble=undef
     usemymalloc=n, bincompat5005=undef
   Compiler:
     cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include 
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
     optimize='-O2',
     cppflags='-fno-strict-aliasing -I/usr/local/include -I/usr/include/gdbm'
     ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.3 2.96-113)', 
gccosandvers=''
     intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
     ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', 
lseeksize=8
     alignbytes=4, prototype=define
   Linker and Libraries:
     ld='cc', ldflags =' -L/usr/local/lib'
     libpth=/usr/local/lib /lib /usr/lib
     libs=-lnsl -lndbm -lgdbm -ldl -lm -lc -lcrypt -lutil -lrt
     perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil -lrt
     libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl.a
     gnulibc_version='2.2.5'
   Dynamic Linking:
     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
     cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'


Characteristics of this binary (from libperl):
   Compile-time options: USE_LARGE_FILES
   Locally applied patches:
         DEVEL18374
   Built under linux
   Compiled at Mar 28 2003 16:15:02
   @INC:
     /usr/local/lib/perl5/5.9.0/i586-linux
     /usr/local/lib/perl5/5.9.0
     /usr/local/lib/perl5/site_perl/5.9.0/i586-linux
     /usr/local/lib/perl5/site_perl/5.9.0
     /usr/local/lib/perl5/site_perl
     .





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