use Data::Dump::Streamer; my $string; while () { chomp; s/\r//; # append a comma to lines not ending in 'Array' or '(' # or being (other than containing whitespace) empty lines $_ .= ',' unless /(Array|\(|^\s*)$/; $string .= $_."\n"; } sub Array { my %h; while(@_) { my ($key) = @{shift(@_)}; $h {$key} = shift(@_); } \%h; } print $string,"\n"; my $ref = eval $string; Dump $ref; __DATA__ Array ( [0] => Array ( [attrs] => Array ( [0] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) [1] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) ) [mesg_id] => 1 [parent_id] => 0 [thread_id] => 1 [cat_id] => 3 [folder_id] => 0 [status] => 4 ) )