use strict; use warnings; use Data::Dump::Streamer; my $contents = <<'BLOCK'; create item "xxx" { remove entry "xxx" add entry "yy" "xxx" { item1=xxxx, item2=xxxx, } add diffenent entry "xxx" { item1=xxx, item2=xxxx, item3=xxx add subitem { item1=xxx, item2=xxx, item3=xxx } add subitem { item1=xxx, item2=xxx, } } another type { item1=xxx } with "xxx" } BLOCK my @tokens = split /\s+/, $contents; my @chunks; push @chunks, ExtractChunks (\@tokens); Dump (\@chunks); sub ExtractChunks { my $tokens = shift; my @chunks; while (@$tokens) { my $token = shift @$tokens; if ($token =~ /{/) { push @chunks, ExtractChunks ($tokens); } elsif ($token =~ /}/) { last; } else { push @chunks, $token; } } return \@chunks; }