List Info

Thread: Empty regular expression




Empty regular expression
user name
2006-12-20 13:12:58
# New Ticket Created by  amlrulezz.ru 
# Please include the string:  [perl #41115]
# in the subject line of all future correspondence about
this issue. 
# <URL: h
ttp://rt.perl.org/rt3/Ticket/Display.html?id=41115 >



This is a bug report for perl from amlrulezz.ru,
generated with the help of perlbug 1.35 running under perl
v5.8.4.


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

Empty regular expression s/...// has a strange behaviour.
Its result depends on previously evaluated regular
expression.
Look at the minimal proof of concept:

#!/usr/bin/perl

{
	print 'Correct result: ';

	&test;
}

{
	print 'Correct result: ';

	my $a = 'c';
	$a =~ s/c//;

	&test;
}

{
	print 'Incorrect result: ';

	my $a = 'd';
	$a =~ s/d//;

	&test;
}

sub test
{
	print(('abc' =~ //) ? 'true' : 'false');
	print "n";
}

Subroutine 'test' contains this strange regexp: sometimes it
returns true, and sometimes false.

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

Configured by Debian Project at Thu Mar 23 21:57:50 UTC
2006.

Summary of my perl5 (revision 5 version 8 subversion 4)
configuration:
  Platform:
    osname=linux, osvers=2.6.15.4,
archname=i386-linux-thread-multi
    uname='linux ninsei 2.6.15.4 #1 smp preempt mon feb 20
09:48:53 pst 2006 i686 gnulinux '
    config_args='-Dusethreads -Duselargefiles
-Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=i386-linux
-Dprefix=/usr -Dprivlib=/usr/share/perl/5.8
-Darchlib=/usr/lib/perl/5.8 -Dvendorprefix=/usr
-Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5
-Dsiteprefix=/usr/local
-Dsitelib=/usr/local/share/perl/5.8.4
-Dsitearch=/usr/local/lib/perl/5.8.4
-Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3
-Dsiteman1dir=/usr/local/man/man1
-Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1
-Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs
-Ud_csh -Uusesfio -Uusenm -Duseshrplib
-Dlibperl=libperl.so.5.8.4 -Dd_dosuid -des'
    hint=recommended, useposix=true, d_sigaction=define
    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='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE
-DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing
-I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS
-DDEBIAN -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='3.3.5 (Debian 1:3.3.5-13)',
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=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc
-lcrypt
    perllibs=-ldl -lm -lpthread -lc -lcrypt
    libc=/lib/libc-2.3.2.so, so=so, useshrplib=true,
libperl=libperl.so.5.8.4
    gnulibc_version='2.3.2'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef,
ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    

---
INC
for perl v5.8.4:
    /etc/perl
    /usr/local/lib/perl/5.8.4
    /usr/local/share/perl/5.8.4
    /usr/lib/perl5
    /usr/share/perl5
    /usr/lib/perl/5.8
    /usr/share/perl/5.8
    /usr/local/lib/site_perl
    .

---
Environment for perl v5.8.4:
    HOME=/home/users/aml
    LANG (unset)
    LANGUAGE (unset)
    LC_ALL=ru_RU.KOI8-R
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
   
PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
    PERL_BADLANG (unset)
    SHELL=/bin/bash

Empty regular expression
user name
2006-12-20 21:47:39
On 12/20/06, via RT aml  rulezz. ru
<perlbug-followupperl.org> wrote:
> # New Ticket Created by  amlrulezz.ru
> # Please include the string:  [perl #41115]
> # in the subject line of all future correspondence
about this issue.
> # <URL: h
ttp://rt.perl.org/rt3/Ticket/Display.html?id=41115 >
>
>
>
> This is a bug report for perl from amlrulezz.ru,
> generated with the help of perlbug 1.35 running under
perl v5.8.4.
>
>
>
------------------------------------------------------------
-----
> [Please enter your report here]
>
> Empty regular expression s/...// has a strange
behaviour.
> Its result depends on previously evaluated regular
expression.
> Look at the minimal proof of concept:
>
> #!/usr/bin/perl
>
> {
>         print 'Correct result: ';
>
>         &test;
> }
>
> {
>         print 'Correct result: ';
>
>         my $a = 'c';
>         $a =~ s/c//;
>
>         &test;
> }
>
> {
>         print 'Incorrect result: ';
>
>         my $a = 'd';
>         $a =~ s/d//;
>
>         &test;
> }
>
> sub test
> {
>         print(('abc' =~ //) ? 'true' : 'false');
>         print "n";
> }
>
> Subroutine 'test' contains this strange regexp:
sometimes it returns true, and sometimes false.

Hi,

This is actually documented behaviour. The empty pattern
when used in
a m// or s/// match operator is special cased to mean the
pattern used
in the last successful match.

If you truely wish to match nothing use the pattern /(?/

I will close this ticket as not-a-bug. Thanks for the report
anyway.

Cheers,
Yves
ps: Yes this can be a trap that probably shouldn't have been
introduced, but its much too late to do anything about it
now.
-- 
perl -Mre=debug -e "/just|another|perl|hacker/"
Empty regular expression
user name
2006-12-22 11:32:58
demerphq wrote:
> If you truely wish to match nothing use the pattern
/(?/

Or just remove the match entirely since that will match
anything.
Empty regular expression
user name
2006-12-22 12:05:11
On 12/22/06, Michael G Schwern <schwerngmail.com> wrote:
> demerphq wrote:
> > If you truely wish to match nothing use the
pattern /(?/
>
> Or just remove the match entirely since that will match
anything.


Well yeah. The reason I mentioned (? was that I
assumed that this
issue came from something like:

  if ($foo=~/$bar/) {  .... }

which suffers from the problem that if $bar is empty it wont
do what
most people expect. Consider:

perl -le"$_='foo'; /(foo)/; $x=''; print qq($1) if
/$x/;"

will output: 'foo'

The way around it is to change the pattern to

  if ($foo=~/(?$bar/) { 
.... }

or to

  if ($foo=~/(?#)$bar/) {  .... }

Personally I dont like the behaviour of the empty pattern
and would
just as soon see it deprecated and removed.

Yves


-- 
perl -Mre=debug -e "/just|another|perl|hacker/"
Empty regular expression
user name
2006-12-22 12:20:11
On 12/22/06, demerphq <demerphqgmail.com> wrote:
> Personally I dont like the behaviour of the empty
pattern and would
> just as soon see it deprecated and removed.

To expand on this moderately, I see the utility of the empty
pattern
being something like:

s//replacment/ if /pata/ or /patb/ or /patc/;

IOW, it allows one access to the pattern part of the
PL_curpm, just as
$1 et all provide access to the startp[] and endp[] from
PL_curpm.
Afaict this is mostly useful when one wants to avoid duped
replacement
text in a situation like above.

Im inclined to say either:

  a) empty pattern match special case should just be removed
  b) empty pattern match special case should only apply when
the
pattern truely is the empty string. A pattern containing an
var that
interpolates to the empty string should not trigger the
behaviour.

Regardless I think that a utility routine like:

  my $qr=last_successful_regex();

would be useful, as would be

   set_last_successful_regex($qr);
   print $1; # prints from the last successful use of $qr.

Similarly an update to the Regexp module would allow one
access to all
of the last match vars through an OO syntax.

Yves




-- 
perl -Mre=debug -e "/just|another|perl|hacker/"
Empty regular expression
user name
2006-12-22 15:33:13
On 22/12/06, demerphq <demerphqgmail.com> wrote:
> Im inclined to say either:
>
>   a) empty pattern match special case should just be
removed

Not without a deprecation cycle, i.e. not before 5.12.

>   b) empty pattern match special case should only apply
when the
> pattern truely is the empty string. A pattern
containing an var that
> interpolates to the empty string should not trigger the
behaviour.

So, when the pattern is constant ? That might make sense.
I've no idea
whether someone actually relied on /$foo/ being a synonym as
// when
$foo was empty. But little surprises me.

> Regardless I think that a utility routine like:
>
>   my $qr=last_successful_regex();
>
> would be useful, as would be
>
>    set_last_successful_regex($qr);
>    print $1; # prints from the last successful use of
$qr.
>
> Similarly an update to the Regexp module would allow
one access to all
> of the last match vars through an OO syntax.

Yes, so those utility routines don't really need to be
hardwired into
the core, right?
Empty regular expression
user name
2006-12-22 18:51:47
On Dec 22, 2006, at 9:33 AM, Rafael Garcia-Suarez wrote:
>> Regardless I think that a utility routine like:
>>
>>   my $qr=last_successful_regex();
>>
>> would be useful, as would be
>>
>>    set_last_successful_regex($qr);
>>    print $1; # prints from the last successful use
of $qr.
>>
>> Similarly an update to the Regexp module would
allow one access to  
>> all
>> of the last match vars through an OO syntax.
>
> Yes, so those utility routines don't really need to be
hardwired into
> the core, right?

I am not sure if it still compiles, but the Regexp package
on CPAN,  
which was written prior to qr//, provides

   my $qr = Regexp->current;

Graham.


Empty regular expression
user name
2006-12-22 19:04:21
On 12/22/06, Graham Barr <gbarrpobox.com> wrote:
> On Dec 22, 2006, at 9:33 AM, Rafael Garcia-Suarez
wrote:
> >> Regardless I think that a utility routine
like:
> >>
> >>   my $qr=last_successful_regex();
> >>
> >> would be useful, as would be
> >>
> >>    set_last_successful_regex($qr);
> >>    print $1; # prints from the last successful
use of $qr.
> >>
> >> Similarly an update to the Regexp module would
allow one access to
> >> all
> >> of the last match vars through an OO syntax.
> >
> > Yes, so those utility routines don't really need
to be hardwired into
> > the core, right?
>
> I am not sure if it still compiles,

Alas no it doesn't. I was going to ask you if you minded if
I bring it
up to date.

> but the Regexp package on CPAN,
> which was written prior to qr//, provides
>
>    my $qr = Regexp->current;
>

Yeah, same idea.

Yves

-- 
perl -Mre=debug -e "/just|another|perl|hacker/"
Empty regular expression
user name
2006-12-22 19:06:31
On 12/22/06, Rafael Garcia-Suarez <rgarciasuarezgmail.com> wrote:
> On 22/12/06, demerphq <demerphqgmail.com> wrote:
> > Im inclined to say either:
> >
> >   a) empty pattern match special case should just
be removed
>
> Not without a deprecation cycle, i.e. not before 5.12.

Naturally. When i said that i meant "should just be
deprecated and
then eventually removed"

> >   b) empty pattern match special case should only
apply when the
> > pattern truely is the empty string. A pattern
containing an var that
> > interpolates to the empty string should not
trigger the behaviour.
>
> So, when the pattern is constant ? That might make
sense. I've no idea
> whether someone actually relied on /$foo/ being a
synonym as // when
> $foo was empty. But little surprises me.

Im sure there are one or two people out there, but IME the
empty
pattern is relatively unused.

> > Regardless I think that a utility routine like:
> >
> >   my $qr=last_successful_regex();
> >
> > would be useful, as would be
> >
> >    set_last_successful_regex($qr);
> >    print $1; # prints from the last successful use
of $qr.
> >
> > Similarly an update to the Regexp module would
allow one access to all
> > of the last match vars through an OO syntax.
>
> Yes, so those utility routines don't really need to be
hardwired into
> the core, right?

Er, im not sure. set_last_succesful_regex() might have to
be. Its very
possible that it can all be done in XS tho.

Yves


-- 
perl -Mre=debug -e "/just|another|perl|hacker/"
[1-9]

about | contact  Other archives ( Real Estate discussion Medical topics )