#!/usr/bin/perl
# https://perlmonks.org/?node_id=1221039
use strict;
use warnings;
$_ = <bw, bliako
[%# placing the '#' immediately inside the directive
tag comments out the entire directive
%]
[% outside %]
[%# placing the '#' immediately inside the directive
tag comments out the entire directive
[% inside %]
%]
after
END
print stripcomments();
sub stripcomments
{
my $answer = '';
$answer .=
/\G\[\%#/gc ? stripcomments() x 0 :
/\G\[\%/gc ? '[%' . stripcomments() =~ s/#.*//gr . '%]' :
/\G\%\]/gc ? return $answer :
/\G./gcs ? $& :
return $answer while 1;
}
####
before
[%
foo = 'bar'
%]
bw, bliako
[% outside %]
after