develooper Front page | perl.perl5.porters | Postings from November 1999

Re: [ID 19991130.003] Assignment to List Breaks \G.

Thread Previous | Thread Next
From:
François Désarménien
Date:
November 30, 1999 08:40
Subject:
Re: [ID 19991130.003] Assignment to List Breaks \G.
Message ID:
3843FD84.7B5D13@club-internet.fr
> Assigning a /gx regexp to a list breaks \G in the following regexp.
> 
>     #! /usr/local/bin/perl -w
> 
>     $_ = 'a 1 b 2 c 3';
> 
>     print "bug\n";
>     ($a, $b) = /^(\w)\s(\d)\s/gx;
>     print "a=$a b=$b\n";
>     ($a, $b) = /\G(\w)\s(\d)/gx;
>     print "a=$a b=$b\n";
> 
>     print "workaround\n";
>     /^(\w)\s(\d)\s/gx;
>     ($a, $b) = ($1, $2);
>     print "a=$a b=$b\n";
>     ($a, $b) = /\G(\w)\s(\d)/gx;
>     print "a=$a b=$b\n";
> 
> Output is
> 
>     bug
>     a=a b=1
>     a=a b=1
>     workaround
>     a=a b=1
>     a=b b=2
> 

Sound correct behaviour to me. From perlop:

       /PATTERN/cgimosx
	[...]
       Options are:

           c   Do not reset search position on a failed match when /g is in effect.
---->      g   Match globally, i.e., find all occurrences.
           i   Do case-insensitive pattern matching.
           m   Treat string as multiple lines.
           o   Compile pattern only once.
           s   Treat string as single line.
           x   Use extended regular expressions.

It just appears that your list is too small to hold the rest of the matches. And as
the match is finished, \G is resetted.
In a scalar context, the 'g' modifier work differently, as it doesn't eat up all matches.
Your "workaroud" is the way to do what you want to.

Hope it helps,

François Désarménien

Thread Previous | 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