develooper Front page | perl.perl5.porters | Postings from March 2013

Re: Do we expect $1 set after concatted regex match?

Thread Previous
From:
demerphq
Date:
March 20, 2013 11:02
Subject:
Re: Do we expect $1 set after concatted regex match?
Message ID:
CANgJU+XTAHawME+GcWyMyzgn1S50_tokyYnxYdkXwV1ZeVnJ1g@mail.gmail.com
On 20 March 2013 11:56, H.Merijn Brand <h.m.brand@xs4all.nl> wrote:
> $ perl -wE'"x".q{abc} =~ m/xab/ and say "OK"'
> OK
> $ perl -wE'"x".q{abc} =~ m/(xa)b/ and say "OK"'
> OK
> $ perl -wE'"x".q{abc} =~ m/(xa)b/ and say $1'
> Use of uninitialized value $1 in say at -e line 1.

This is a precedence issue with concatenation and the regex operator.
It is being parsed as

perl -wE'( "x" . ( q{abc} =~ m/(xa)b/ ) ) and say $1'

and not as

perl -wE'( ( "x" . q{abc} ) =~ m/(xa)b/ ) and say $1'

Observe:

$ perl -Mre=debug -wE'"x".q{abc} =~ m/(xa)b/ and say $1'
Compiling REx "(xa)b"
Final program:
   1: OPEN1 (3)
   3:   EXACT <xa> (5)
   5: CLOSE1 (7)
   7: EXACT <b> (9)
   9: END (0)
anchored "xab" at 0 (checking anchored) minlen 3
Guessing start of match in sv for REx "(xa)b" against "abc"
Did not find anchored substr "xab"...
Match rejected by optimizer
Use of uninitialized value $1 in say at -e line 1.

Freeing REx: "(xa)b"

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

Thread Previous


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