use strict; use warnings; use Data::Dumper; $Data::Dumper::Useqq = 1; my $tt = <<'TT'; [% # this is a comment to the end of line foo = 'bar' %]

You might have an array in your TT

[% foo = bar[5]; %]

bw, bliako

[%# placing the '#' immediately inside the directive tag comments out the entire directive %] TT my @parts = ($tt =~ /\G( (?:[^\\\[]+) # not a template, not a backslash |(?:[\\].) # an escaped whatever |(?:[\[][^%]) # not a template, [ followed by whatever |(?:\[%.*?%\]) # within a TT template ) /msgx); @parts = map { s!\s+#.*$!!gm; $_ } # comments up to EOL map { /^\[%#/ ? "" : $_ } # TT comments @parts; warn Dumper \@parts;