use strict; use warnings; while () { s/\[\[(\w+)/parseHereDoc ("$1")/e if /\[\[\w+/; print; } sub parseHereDoc { my $id = shift; my $str = '"'; while () { last if /^$id$/; $str .= $_; } return $str . '"'; } __DATA__ var = [[END . "other stuff"; heredoc data END