develooper Front page | perl.perl5.porters | Postings from January 2005

Re: /(?{...})/ bug?

Thread Previous
From:
hv
Date:
January 29, 2005 08:34
Subject:
Re: /(?{...})/ bug?
Message ID:
200501291640.j0TGebB18614@zen.crypt.org
Dave Mitchell <davem@iabyn.com> wrote:
:(*) This behaviour is documented in Camel III: local() within a (?{})
:doesn't undo at the end of the block, rather it undoes at the the end of
:the regex. It's never been clear to me whether this was deliberate,
:desired behaviour, or just a side-effect of the implementation. Personally
:I dislike it.

I think the point is that it lets you do things like:
  m{
    head
    (?:
      pattern
      (?{ local $once = $once + 1 })
      more
    )*
    tail
    (??{ $once == 1 ? $succeed : $fail })
  }x;

.. where the local is undone only at the end of the match *or* by
backtracking. I use this type of thing heavily in my crossword
solver (http://www.crypt.org/hv/perl/word), eg:

  zen% word -Da a[bc][cd]
  /^(?{
    $d[0] = [ [ 1, 1 ], [ 1, 1 ], [ 1, 1 ] ];
    $e[0] = [ (0) x 3 ];
  })(?:(?:a(??{ 
    local $e[0][0] = $e[0][0] + 1;
    $e[0][0] > $d[0][0][1] ? $fail : $succeed
  })|[cd](??{ 
    local $e[0][1] = $e[0][1] + 1;
    $e[0][1] > $d[0][1][1] ? $fail : $succeed
  })|[bc](??{ 
    local $e[0][2] = $e[0][2] + 1;
    $e[0][2] > $d[0][2][1] ? $fail : $succeed
  }))*(??{
    (grep $e[0][$_] < $d[0][$_][0], 0 .. 2) ? $fail : $succeed
  }))\z/oi
  zen% 

-D: dump the regexp and quit without attempting to search for words
-a: allow any anagram of the supplied pattern

(Note that this construct is specifically needed when the pattern
includes distinct overlapping character classes.)

Hugo

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About