Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

[perl #51848] Deparse interpolation in regex literal

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


perlbug-followup at perl

Mar 18, 2008, 8:12 AM

Post #1 of 6 (123 views)
Permalink
[perl #51848] Deparse interpolation in regex literal

# New Ticket Created by Zsban Ambrus
# Please include the string: [perl #51848]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=51848 >


This is a bug report for perl from ambrus[at]math.bme.hu,
generated with the help of perlbug 1.36 running under perl 5.10.0.


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

The following deparse command

perl -MO=Deparse -e '/${x}y/'

gives a wrong output:

/$xy/;
-e syntax OK

This is wrong, because the original script interpolates the variable $x to
the regex, whereas the deparse output interpolates the variable $xy.
Compare the following:

$ perl -MO=Deparse -e '"${x}y"'
"${x}y";
-e syntax OK
$ perl -MO=Deparse,-q -e '"${x}y"'
$x . 'y';
-e syntax OK
$ perl -MO=Deparse -e '/${x}y/'
/$xy/;
-e syntax OK
$ perl -MO=Deparse,-q -e '/${x}y/'
/$xy/;
-e syntax OK

I believe the results for regex literals should be analogous to the string
literals: an interpolated variable name should be braced if the expression
would be ambiguous otherwise. I'm not sure what the -q option should do in
the regex case.

To prove that the regex literal /${x}y/ is parsed right, so this is a
deparse bug, not a parser bug, either examine the Concise output, or
try this:

$ perl -e '$x = "m"; print qr/${x}y/, $/'
(?-xism:my)
$ perl -MO=Deparse -e '$x = "m"; print qr/${x}y/, $/'
$x = 'm';
print qr/$xy/, $/;
-e syntax OK

By the way, this seems to happen in perl 5.10.0, but not in 5.8.8.

Thanks in advance,

Ambrus


[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
category=library
severity=low
---
Site configuration information for perl 5.10.0:

Configured by ambrus at Tue Dec 18 19:35:54 CET 2007.

Summary of my perl5 (revision 5 version 10 subversion 0) configuration:
Platform:
osname=linux, osvers=2.6.20.6, archname=i686-linux
uname='linux king 2.6.20.6 #3 smp tue may 29 09:42:52 cest 2007
i686 gnulinux '
config_args=''
hint=recommended, useposix=true, d_sigaction=define
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='gcc', ccflags ='-fno-strict-aliasing -pipe
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O2',
cppflags='-fno-strict-aliasing -pipe -I/usr/local/include'
ccversion='', gccversion='4.1.1', 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='gcc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib /usr/lib64
libs=-lnsl -ldl -lm -lcrypt -lutil -lc
perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
libc=/lib/libc-2.3.6.so, so=so, useshrplib=false, libperl=libperl.a
gnulibc_version='2.3.6'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib'

Locally applied patches:


---
@INC for perl 5.10.0:
/usr/local/lib/perl5/5.10.0/i686-linux
/usr/local/lib/perl5/5.10.0
/usr/local/lib/perl5/site_perl/5.10.0/i686-linux
/usr/local/lib/perl5/site_perl/5.10.0
/usr/local/lib/perl5/site_perl/5.8.8
/usr/local/lib/perl5/site_perl
.

---
Environment for perl 5.10.0:
HOME=/home/ambrus
LANG (unset)
LANGUAGE=en_HU:en_US:en_GB:en
LC_CTYPE=hu_HU
LD_LIBRARY_PATH (unset)
LOGDIR (unset)
PATH=/home/ambrus/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
PERL_BADLANG (unset)
SHELL=/bin/bash


rgarciasuarez at gmail

May 16, 2008, 8:07 AM

Post #2 of 6 (100 views)
Permalink
Re: [perl #51848] Deparse interpolation in regex literal [In reply to]

2008/3/18 via RT Zsban Ambrus <perlbug-followup[at]perl.org>:
> The following deparse command
>
> perl -MO=Deparse -e '/${x}y/'
>
> gives a wrong output:
>
> /$xy/;

The optree changed, op_concat is no longer used here:

$ perl5.8.8 -MO=Concise -e '/${x}y/'
9 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v ->3
8 </> match() vK/RTIME ->9
7 <|> regcomp(other->8) sK/1 ->8
3 <1> regcreset sK/1 ->4
6 <2> concat[t2] sK/2 ->7
- <1> ex-rv2sv sK/1 ->5
4 <#> gvsv[*x] s ->5
5 <$> const[PV "y"] s ->6
-e syntax OK

$ bleadperl -MO=Concise -e '/${x}y/'
9 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
8 </> match() vK/RTIME ->9
7 <|> regcomp(other->8)[t1] sKS/1 ->8
3 <1> regcreset sK/1 ->4
- <1> ex-list sK ->7
4 <0> pushmark s ->5
- <1> ex-rv2sv sK/1 ->6
5 <$> gvsv(*x) s ->6
6 <$> const(PV "y") s ->7
-e syntax OK

Once this is remarked, the fix is trivial. Just get the escaping code
from re_dq and put in in the deparse routine for regcomp :

--- ext/B/B/Deparse.pm 2008-04-21 17:14:12.000000000 +0200
+++ lib/B/Deparse.pm 2008-05-16 17:02:52.000000000 +0200
@@ -4156,7 +4156,16 @@
my $str = '';
$kid = $kid->first->sibling;
while (!null($kid)) {
- $str .= $self->re_dq($kid, $extended);
+
+ my $first = $str;
+ my $last = $self->re_dq($kid, $extended);
+ # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
+ ($last =~ /^[A-Z\\\^\[\]_?]/ &&
+ $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
+ || ($last =~ /^[{\[\w_]/ &&
+ $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
+ $str = $first . $last;
+
$kid = $kid->sibling;
}
return $str, 1;

But of course that patch is Wrong because I cutted and pasted instead
of Properly Refactored. And also it lacks tests.


rgarciasuarez at gmail

May 16, 2008, 8:10 AM

Post #3 of 6 (99 views)
Permalink
Re: [perl #51848] Deparse interpolation in regex literal [In reply to]

2008/5/16 Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com>:
> The optree changed, op_concat is no longer used here:

(I wonder why/how, by the way)


demerphq at gmail

May 16, 2008, 1:14 PM

Post #4 of 6 (96 views)
Permalink
Re: [perl #51848] Deparse interpolation in regex literal [In reply to]

2008/5/16 Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com>:
> 2008/5/16 Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com>:
>> The optree changed, op_concat is no longer used here:
>
> (I wonder why/how, by the way)
>

My bet is its something that Dave did.

Yves

--
perl -Mre=debug -e "/just|another|perl|hacker/"


davem at iabyn

May 17, 2008, 4:26 PM

Post #5 of 6 (91 views)
Permalink
Re: [perl #51848] Deparse interpolation in regex literal [In reply to]

On Fri, May 16, 2008 at 10:14:50PM +0200, demerphq wrote:
> 2008/5/16 Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com>:
> > 2008/5/16 Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com>:
> >> The optree changed, op_concat is no longer used here:
> >
> > (I wonder why/how, by the way)
> >
>
> My bet is its something that Dave did.

Yes, it was the first step of my long-intended 'fix embedded code in
patterns' thinggy. The idea is that the regex compiler can be
presented with a list of things (included snippets of compiled code),
rather than having everything folded into a single string before it gets a
chance to look at it.

--
"There's something wrong with our bloody ships today, Chatfield."
-- Admiral Beatty at the Battle of Jutland, 31st May 1916.


rgarciasuarez at gmail

May 18, 2008, 1:37 AM

Post #6 of 6 (84 views)
Permalink
Re: [perl #51848] Deparse interpolation in regex literal [In reply to]

2008/5/16 Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com>:
> But of course that patch is Wrong because I cutted and pasted instead
> of Properly Refactored. And also it lacks tests.

Now properly fixed as change #33851

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.