#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; my @lines = ; chomp @lines; my @list = ([q{div}, {id => q{article}}]); my $currentList_r = \@{$list[0]}; my $currentDiv=""; my @listStack; LINE: for my $line (@lines){ chomp $line; if($line=~/^;([^;]+)_start;$/) { $currentDiv=$1; my $divList_r=['div',{class=>$1}]; push @listStack, $currentList_r; $currentList_r=$divList_r; next LINE; } if($line=~/^;([^;]+)_end;$/) { die "Tag mismatch between start ($currentDiv) and end ($1)" unless $1 eq $currentDiv; $currentDiv=""; die "end with no matching start!" unless scalar @listStack; push @{$listStack[-1]}, $currentList_r; $currentList_r = pop @listStack; next LINE; } my ($tag, $txt) = $line =~ /^;([^;]+);(.*)/; push @{$currentList_r}, [$tag, $txt]; } print Dumper(\@list), __DATA__ ;p;one ;greybox_start; ;h2;two ;greybox_end; ;p;three