On Fri, Oct 21, 2011 at 1:52 PM, Rob Dixon <rob.di...@gmx.com> wrote:
> It is nothing to do with matched patterns overlapping. As I tried to
> describe, it is the designed behaviour of m//g in scalar context to find
> just one match, searching from the end of the previous match if it was
> successful, or restarting from the beginning of the string if failed.
>
> This behaviour is independent of any surrounding loop construct, and is
> tied to the target string that is being searched. The position where a
> m//g pattern match will start looking is reflected in the pos($target)
> function, which is undef before any m//g match or after a failed match,
> otherwise it is set to the value of $+[0] - the offset of the end of the
> matched string.

Thanks for the explanation, Rob. :)

use strict;
use warnings;

use v5.010;

my $data = 'aaa bbb ccc';

say $data;

for my $pattern (qw(bbb aaa ccc))
{
    say join ' ', pos($data) // 'undef', $pattern,
            scalar $data =~ /$pattern/g;
}

__END__

Would not have expected that. Neat. :)

Regards,


-- 
Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to