my $root = {}; my $node = $root; my @stack; while () { chomp; if (/^\s*BEGIN\s+(.*)/) { my $child = {}; $child->{_type} = $1; push(@{$node->{_children}}, $child); push(@stack, $node); $node = $child; } elsif (/^\s*END\s+(.*)/) { # check: $node->{_type} eq $1 $node = pop(@stack); } elsif (/^\s*(\S+)\s+(.*)/) { # remove quotes from $2 here? $node->{$1} = $2; } else { die "unexpected input: $_" } } use Data::Dumper; print Dumper($root); #### $VAR1 = { '_children' => [ { 'NLSLocale' => '",,,,"', 'JobVersion' => '"50.0.0"', 'MetaBag' => '"CMetaProperty"', 'OLEType' => '"CJobDefn"', 'Container' => '"V0"', 'NULLIndicatorPosition' => '"0"', '_type' => 'DSRECORD', '_children' => [ { 'Prompt' => '"/home/migration/Dev root "', 'Default' => '"/home/migration/Dev"', 'ParamScale' => '"0"', 'ParamLength' => '"0"', '_type' => 'DSSUBRECORD', 'ParamType' => '"0"', 'Name' => '"ROOT"' }, { 'Prompt' => '"Business Unit, ie WHUB"', 'Default' => '"CDBS"', 'ParamScale' => '"0"', 'ParamLength' => '"0"', '_type' => 'DSSUBRECORD', 'ParamType' => '"0"', 'Name' => '"SITE"' }, { 'Prompt' => '"Area of Work ie AP"', 'Default' => '"AP"', 'ParamScale' => '"0"', 'ParamLength' => '"0"', '_type' => 'DSSUBRECORD', 'ParamType' => '"0"', 'Name' => '"AOW"' }, { 'Prompt' => '"DMR/Spec ie Vendors"', 'Default' => '"Vendors"', 'ParamScale' => '"0"', 'ParamLength' => '"0"', '_type' => 'DSSUBRECORD', 'ParamType' => '"0"', 'Name' => '"DMR"' }, { 'Owner' => '"APT"', 'Value' => '"#DSProjectARTOptions#"', '_type' => 'DSSUBRECORD', 'Name' => '"AdvancedRuntimeOptions"' } ], 'Readonly' => '"0"', 'NextID' => '"194"', 'ControlAfterSubr' => '"0"', 'TimeModified' => '"00.00.01"', 'CenturyBreakYear' => '"30"', 'Parameters' => '"CParameters"', 'JobType' => '"0"', 'DateModified' => '"1899-12-30"', 'Identifier' => '"ROOT"', 'FullDescription' => '"The first part of the routine gathers data from the ABAP which extracts the necessary data from the SAP tables KNA1 and KNB1 (NB the keys of the link between KNA1 and KNB1 will form the basis of all the ABAP queries)."', 'Description' => '"Collates all of the data for the customer master migration."', 'Name' => '"AP_CDBS_Vendor_Summary"', 'Category' => '"1.FSS\\\\2.AP\\\\6.CDBS\\\\1.Vendors\\\\3.Reports"', 'IsTemplate' => '"0"' } ] };