On Wed, 18 Sep 2002, Josh Jore wrote:

> On Wed, 18 Sep 2002, Damian Conway wrote:
> 
> > > Would it be correct for this to print 0? Would it be correct for this
> > > to print 2?
> > >
> > >   my $n = 0;
> > >   "aargh" =~ /a* { $n++ } aargh/;
> > >   print $n;
> >
> > Yes. ;-)
> 
> Wouldn't that print 2 if $n is lexical and 0 if it's localized? Or are
> lexicals localized now?

Well, { $n++ } is within the lexical scope of $n, so it doesn't matter.  
What matters is whether $n++ was hypotheticalized like so:

        "aargh" =~ /a* { let $n++ } aargh/  # can it work that way?

Then it would either print 1 or 0, because if it backtracked, the ++ would 
be undone.  If the change is adopted that you can't optimize when there's 
a closure in the middle of the optimization, it would print 1.

> > > What possible outputs are legal for this:
> > >
> > >   "aaa" =~ /( a { print 1 } | a { print 2 })* { print "\n" } x/
> 
> I take it that what I've learned from _Mastering_Regular_Expressions_
> doesn't quite apply here? From that interpretation I'd think it'd print
> "111\n" since the second part of the alternation wouldn't be tried.

The first time through, yes.  But then it tries to match the "x", and says 
"oops, that's not what I have" and backtracks.  That tries the second of 
the alternation, which doesn't work either.  So it backtracks so the * is 
only getting two of the first one, et cetera...

Or are you talking about something else from Mastering Regular 
Expressions?  Like some kind of optimization that happens?

Luke

Reply via email to