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

[perl #38639] Problem with non-global variables within ?{} code in regexps

From:
Richard Clayton
Date:
February 26, 2006 13:31
Subject:
[perl #38639] Problem with non-global variables within ?{} code in regexps
Message ID:
rt-3.0.11-38639-130630.8.63122650206918@perl.org
# New Ticket Created by  Richard Clayton 
# Please include the string:  [perl #38639]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=38639 >



This is a bug report for perl from richard@highwayman.com,
generated with the help of perlbug 1.35 running under perl v5.8.7.


-----------------------------------------------------------------
[Please enter your report here]


The problem is in setting variable values in ?{} sections of a regexp

I am doing this within a foreach loop and the value is set the first time
around the loop, but the second time the value is not set.

Further experimentation has shown this works OK if the variable is global
(declared at the outermost level) but not if the variable is declared
within the foreach loop itself.

Possibly it's not meant to work for non-global variables... in which case
the bug is that it works once :) and doesn't fail every time around.

Fairly simple demo program below: the output I get is:

C:\work\scan\logs>perl -w test.pl
Test 1
T = 'abc'
----
T = 'abc'
----
Test 2
S = 'abc'
----
Use of uninitialized value in concatenation (.) or string at test.pl line 52.
S = ''
----

C:\work\scan\logs>


and here's the program that gives the effect.  I've tried it on several
different versions of Perl, both on Windows and Linux and get the same
effect on all of them :(

Hope that's all clear -- if not then please just ask.

Thanks

---example program---
#
# seems to be a Perl bug connected with the use of non-global variables
# in ?{} constructs within regexps
#
# Richard Clayton <richard@highwayman.com>
# 26 Feb 2006
#
# from the documentation I think the problem is the second time around
# when the local variable isn't set ... but it may be that it's not
# supposed to work, in which case the first time around is the problem :)
#

use strict;

print "Test 1\n";

# first test ... the variable in the ?{} within the regexp is global

my @a;

$a[0] = "foobar";
$a[1] = "foobar";

my $T;

foreach my $a (@a)
{
   if ($a =~ /^foobar(?{$T = "abc"})$/)
   {
      print "T = '$T'\n";
   }

   print "----\n";
}

print "Test 2\n";

# second test ... the variable in the ?{} within the regexp is local
# and on the SECOND time around the loop it isn't set

my @b;

$b[0] = "foobar";
$b[1] = "foobar";

foreach my $b (@b)
{
   my $S;

   if ($b =~ /^foobar(?{$S = "abc"})$/)
   {
      print "S = '$S'\n";
   }

   print "----\n";
}

# end of example
---end of example program--


[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=medium
---
Site configuration information for perl v5.8.7:

Configured by builder at Wed Nov  2 08:44:18 2005.

Summary of my perl5 (revision 5 version 8 subversion 7) configuration:
  Platform:
    osname=MSWin32, osvers=5.0, archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cl', ccflags ='-nologo -Gf -W3 -MD -Zi -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DNO_HASH_SEED -DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX',
    optimize='-MD -Zi -DNDEBUG -O1',
    cppflags='-DWIN32'
    ccversion='12.00.8804', gccversion='', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='__int64', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='link', ldflags ='-nologo -nodefaultlib -debug -opt:ref,icf  -libpath:"C:\apps\perl584\lib\CORE"  -machine:x86'
    libpth=\lib
    libs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib  comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib msvcrt.lib
    perllibs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib  comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib msvcrt.lib
    libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl58.lib
    gnulibc_version='undef'
  Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -debug -opt:ref,icf  -libpath:"C:\apps\perl584\lib\CORE"  -machine:x86'

Locally applied patches:
    ACTIVEPERL_LOCAL_PATCHES_ENTRY
    Iin_load_module moved for compatibility with build 806
    Avoid signal flag SA_RESTART for older versions of HP-UX
    PerlEx support in CGI::Carp
    Less verbose ExtUtils::Install and Pod::Find
    instmodsh upgraded from ExtUtils-MakeMaker-6.25
    Patch for CAN-2005-0448 from Debian with modifications
    Upgrade to Time-HiRes-1.76
    25774 Keys of %INC always use forward slashes
    25747 Accidental interpolation of $@ in Pod::Html
    25362 File::Path::mkpath resets errno
    25181 Incorrect (X)HTML generated by Pod::Html
    24999 Avoid redefinition warning for MinGW
    24699 ICMP_UNREACHABLE handling in Net::Ping
    21540 Fix backward-compatibility issues in if.pm

---
@INC for perl v5.8.7:
    c:/apps/Perl584/lib
    c:/apps/Perl584/site/lib
    .

---
Environment for perl v5.8.7:
    HOME (unset)
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=c:\apps\Perl584\bin\;c:\apps\batch;c:\apps\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\PROGRA~1\COMMON~1\SONICS~1\;C:\Program Files\Microsoft Office\OFFICE11\Business Contact Manager\IM;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft Office\OFFICE11\Business Contact Manager\;c:\cygwin\bin\;C:\altera\quartus41\bin;C:\altera\quartus41\eda\sim_lib\excalibur\swift\lib\pcnt.lib;C:\altera\quartus41\bin;c:\wince420\platform\EPXA1withDisplay\bin;C:\Program Files\Microsoft SDK\Bin\.;C:\Program Files\Microsoft SDK\Bin\WinNT\.;C:\Program Files\Microsoft Platform SDK for Windows XP SP2\Bin\.;C:\Program Files\Microsoft Platform SDK for Windows XP SP2\Bin\WinNT\.;C:\Program Files\MySQL\MySQL Server 4.1\bin;C:\Program Files\Network Associates\PGPNT;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\
    PERL_BADLANG (unset)
    SHELL (unset)




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