my @chunks = split(//, $data); while (@chunks) { # ought to splice here my ($html, $inc) = (shift @chunks, shift @chunks); # print $html to file # include $inc if possible }