On Sun, Jun 24, 2001 at 07:13:47PM -0300, Rogério Brito wrote:
>       1 - How does the Perl grammar interprets the following statements:
> 
>           $n = () = m/string/g;
> 
>           In particular, is "()" an lvalue in Perl?

Yes, it is.  () is a list assignment with no values.  A list assignment in
scalar context returns the number of elements produced by the expression on
the right side of the assignment (as documented in perldata).

 
>       2 - Why does the following snippet prints 3 and not 4?
> 
>           $_ = "ana e mariana gostam de banana";
>             $n = () = m/ana/g;
>             print "$n\n";
> 
>           Perl seems to not be finding occurences of overlapped
>           patterns (as I would expect from the implementation of a
>           Boyer-Moore algorithm -- which I read somewhere that Perl
>           uses when dealing with simple patterns).

The regex engine, by the time it has gotten past the 'ana' after the 'b' has
bumped past the 'a' that would produce another 'ana'.  Effectively, this
means that the string being examined after the first 'ana' in banana is
'na', which obviously doesn't match 'ana'.

This behaviour of m//g, not matching overlaps, is desirable and correct. 
m//g is usually used for parsing, not counting occurrences, and parsing out
overlaps as individual tokens would be a Very Bad Thing.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to