#!/usr/bin/perl
# https://perlmonks.org/?node_id=1221039
use strict;
use warnings;
my $someTTstring = <<END;
before
[% # this is a comment to the end of line
foo = 'bar'
%]
<p>bw, bliako</p>
[%# 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($someTTstring);
sub stripcomments
{
@_ and local $_ = shift;
my $answer = '';
$answer .=
/\G\[\%#/gc ? stripcomments() x 0 :
/\G\[\%/gc ? '[%' . stripcomments() =~ s/#.*//gr . '%]' :
/\G\%\]/gc ? return $answer :
/\G./gcs ? $& :
return $answer while 1;
}
Like this?
|