in reply to balanced parens regexp hangs - solved

In addition to moritz's comment, be aware that the  //x modifier used in your posted code with the  /module (\w+) \{ ($parens) \};?/gx regex means that whitespace is a comment, so you are looking for "the substring 'module', followed immediately by one or more \w characters, followed immediately by a '{' character, etc..."

As moritz said, lexical variables do not play well with the  (??{ code }) Extended Patterns variant; it is really intended to work with package variables. The following works for me (note that  $parens is a fully local-ized package variable):

use warnings; use strict; use constant DENT => ' '; our $parens; my $text = do { local $/; <DATA> }; sub parse { my ($text, $indent) = @_; $indent = 0 unless defined $indent; local $parens = qr{ (?: [^{}]+? | \{ (??{$parens}) \} )* }xms; while ($text =~ /module \s+ (\w+) \s* \{ ($parens) \} ;? /gx) { my ($name, $content) = ($1, $2); print DENT x $indent, qq(subgraph $name { \n); ++$indent; parse($content, $indent); print DENT x $indent, qq(} \n); --$indent; } } # print $text; # FOR DEBUG print "digraph G\n{\n"; parse($text); print "}\n"; __DATA__ module abc { module def { module ghi { module jkl {} }; }; };
Output:
digraph G { subgraph abc { subgraph def { subgraph ghi { subgraph jkl { } } } } }
Updates:
  1. Run under ActiveState Perl 5.8.2.808.
  2. Quoted regex narration for clarity.

Replies are listed 'Best First'.
Re^2: balanced parens regexp hangs
by ikegami (Patriarch) on Mar 27, 2009 at 01:01 UTC
    There's no reason to have the our where it is. Use the following instead:
    local our $parens = qr{ ... }xms;