in reply to Implementing variable-width negative lookbehind assertions?

I can offer you some simplification.

First, the regex2 sub doesn't have to use eval, as the normal closure mechanism combined with //o should give you the semantics you have now. Also, you're going to the trouble of initializing your returned count to zero, then if it stays zero, you're returning undef. Better just to leave it uninitialized and use ++ on it; the results will be as you want. Thus:

sub regex2 { my $pat = shift; sub { my $count; ++$count while /$pat/iogx; $count }; }

    -- Chip Salzenberg, Free-Floating Agent of Chaos

Replies are listed 'Best First'.
(tye)Re: Implementing variable-width negative lookbehind assertions?
by tye (Sage) on Jun 24, 2001 at 10:57 UTC

    Interesting. The subroutine only gets compiled once but the regex gets compiled once for each closure created. That was a surprise to me at first but then the regex can't be compiled when the subroutine gets compiled ($pat isn't set then) so it gets compiled when it is first used (or is it actually compiled when the closure is created?)...

    I thought that the regex state was attached to a node in the parse tree and I'd expect the closures to all be sharing the same node for that regex...

    Care to go into more detail on why/how this works? Was this regex/closure interaction specifically implemented or did it fall out of the general design?

            - tye (but my friends call me "Tye")