http://www.perlmonks.org?node_id=102650


in reply to Reading an arbitrary hierarchy into a hash tree

Just for the fun of it, here's a way which translates your text into XML, then uses XML::Simple on the result:
use strict; use Data::Dumper; use XML::Simple; my $str = do { local $/=undef; <DATA> }; LOOP: { for ($str) { # Make start/end tags for current level s#^(\w+)(.*?)(?=^[\w<]|\Z)#<$1>$2\n</$1>\n#smg; # Process next level s/^\t//mg or last LOOP; } redo LOOP; } my $xs = XML::Simple->new(suppressempty=>''); # Add root element my $href = $xs->XMLin("<root>$str</root>"); print Dumper($href); __DATA__ abc def ghi jkl mno pqr