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

[perl #32840] $^R value lost in (?:...)? constructs

Thread Next
From:
david @ landgren . net
Date:
December 4, 2004 17:27
Subject:
[perl #32840] $^R value lost in (?:...)? constructs
Message ID:
rt-3.0.11-32840-101759.12.3584515990184@perl.org
# New Ticket Created by  david@landgren.net 
# Please include the string:  [perl #32840]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org:80/rt3/Ticket/Display.html?id=32840 >


This is a bug report for perl from david@landgren.net,
generated with the help of perlbug 1.35 running under perl v5.8.6.


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

It is possible to use simple expressions in (?{...}) constructs,
such as (?{123}), the idea being that they could be picked up
afterwards by reading the contents of $^R.

When optional (?:...)? patterns are used, the engine appears
to become confused and $^R is set to the value of the expression
prior to the optional part. It can be shown that the code blocks
are in fact executed. Code with side effects (pushing values onto
an array) is run, and the expected results are observed after the
match. The problem occurs only for simple expressions.

This behaviour is seen in 5.005_03, 5.8.5 and 5.8.6.

There do not appear to be any tests that check how $^R should
behave. The following file is an attempt to improve matters.

(The following tests all pass except for the following:

    not ok 8 - $^R == 8
    #     Failed test (./vr at line 21)

$^R is in fact equal to 7).

__TEST_FILE_BEGIN__
use Test::Simple tests => 26;

$^R = undef;
ok( 'a'  =~ /^a(?{1})(?:b(?{2}))?/, 'a =~ ab?' );
ok( $^R == 1, '$^R == 1' );

$^R = undef;
ok( 'abc' !~ /^a(?{3})(?:b(?{4}))$/, 'abc !~ a(?:b)$' );
ok( !defined $^R, 'undef 2nd' );

$^R = undef;
ok( 'ab' =~ /^a(?{5})b(?{6})/, 'ab =~ ab' );
ok( $^R == 6, '$^R == 6' );

$^R = undef;
ok( 'ab'  =~ /^a(?{7})(?:b(?{8}))?/, 'ab =~ ab?' );
ok( $^R == 8, '$^R == 8' );

$^R = undef;
ok( 'ab'  =~ /^a(?{9})b?(?{10})/, 'ab =~ ab? (2)' );
ok( $^R == 10, '$^R == 10' );

my @ar;
ok( 'ab'  =~ /^a(?{push @ar,11})(?:b(?{push @ar,12}))?/, 'ab =~ ab?' );
ok( scalar(@ar) == 2, 'nr pushed ok' );
ok( ($ar[0] == 11 and $ar[1] == 12), 'push contents ok' );

$^R = undef;
ok( 'a' !~ /^a(?{13})b(?{14})/, 'a !~ ab' );
ok( !defined $^R, 'undef 3rd' );

@ar = ();
ok( 'a' !~ /^a(?{push @ar,15})b(?{push @ar,16})/, 'a !~ ab (push)' );
ok( scalar(@ar) == 0, 'none pushed ok' );

@ar = ();
ok( 'abc' !~ /^a(?{push @ar,17})b(?{push @ar,18})$/, 'abc !~ ab$ (push)' );
ok( scalar(@ar) == 0, 'none pushed ok' );

use vars '@var';

ok( 'ab'  =~ /^a(?{push @var,19})(?:b(?{push @var,20}))?/, 'ab =~ ab?' );
ok( scalar(@var) == 2, 'nr pushed ok' );
ok( ($var[0] == 19 and $var[1] == 20), 'push contents ok' );

@var = ();
ok( 'a' !~ /^a(?{push @var,21})b(?{push @var,22})/, 'a !~ ab (push)' );
ok( scalar(@var) == 0, 'none pushed ok' );

@var = ();
ok( 'abc' !~ /^a(?{push @var,23})b(?{push @var,24})$/, 'abc !~ ab$ (push)' );
ok( scalar(@var) == 0, 'none pushed ok' );
__TEST_FILE_END__

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

Configured by david at Sat Dec  4 17:37:22 CET 2004.

Summary of my perl5 (revision 5 version 8 subversion 6) configuration:
  Platform:
    osname=freebsd, osvers=4.10-stable, archname=i386-freebsd
    uname='freebsd relay.bpinet.com 4.10-stable freebsd 4.10-stable #3: wed jul 28 13:07:34 cest 2004 root@relay.bpinet.com:usrobjusrsrcsysrelay i386 '
    config_args=''
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=y, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -I/usr/local/include',
    optimize='-O',
    cppflags='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -I/usr/local/include'
    ccversion='', gccversion='2.95.4 20020320 [FreeBSD]', 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 ='-Wl,-E  -L/usr/local/lib'
    libpth=/usr/lib /usr/local/lib
    libs=-lm -lcrypt -lutil -lc
    perllibs=-lm -lcrypt -lutil -lc
    libc=, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-DPIC -fPIC', lddlflags='-shared  -L/usr/local/lib'

Locally applied patches:
    

---
@INC for perl v5.8.6:
    /usr/local/lib/perl5/5.8.6/i386-freebsd
    /usr/local/lib/perl5/5.8.6
    /usr/local/lib/perl5/site_perl/5.8.6/i386-freebsd
    /usr/local/lib/perl5/site_perl/5.8.6
    /usr/local/lib/perl5/site_perl/5.8.5/i386-freebsd
    /usr/local/lib/perl5/site_perl/5.8.5
    /usr/local/lib/perl5/site_perl/5.005
    /usr/local/lib/perl5/site_perl
    .

---
Environment for perl v5.8.6:
    HOME=/home/david
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/bin:/bin:/usr/local/bin:.
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/bash


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