use Regexp::Common; sub t2p { local $_ = $_[0]; my $last = my $root = []; my @prev; pos($_)=0; while( length > pos ){ if( m{ \G \s+ }gscx ){ next; # ignore space } elsif( m{ \G \{ }gscx ){ push @$last, my $new = []; push @prev, $last; $last = $new; } elsif( m{ \G \} }gscx ){ $last = pop @prev; #~ } elsif( m{ \G ( [^\s\{\}]+ ) }gscx ){ } elsif( m{ \G ( $RE{quoted} ) }gscx ){ push @$last, $1; #~ } elsif( m{ \G ( [^\s\{\}]+ ) }gscx ){ } elsif( m{ \G ( [^\{\}\\]+ | \\[\{\}] ) }gscx ){ push @$last, $1; } elsif( m{ \G (.) }gscx ){ push @$last, { error => $1 }; } } $root }