in reply to balanced parens regexp hangs - solved
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):
Output: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 {} }; }; };
Updates:digraph G { subgraph abc { subgraph def { subgraph ghi { subgraph jkl { } } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: balanced parens regexp hangs
by ikegami (Patriarch) on Mar 27, 2009 at 01:01 UTC |