use v5.14; use strictures; package Parser { use Moo 1.006000; use Types::Standard qw( RegexpRef ArrayRef ); use Text::Balanced qw( extract_bracketed ); use HTML::Entities qw( encode_entities ); use namespace::autoclean; my $Allowance = RegexpRef->plus_coercions( ArrayRef, sub { qr/${\( join "|", map quotemeta, @$_ )}/ }, ); has allowed_tags => ( is => 'ro', isa => $Allowance, coerce => 1, builder => sub { [qw(A ABBR ACRONYM B BIG CITE CODE DFN EM I KBD Q SAMP SMALL SPAN STRONG SUB SUP TT VAR)] }, ); sub print { my $self = shift; $self = $self->new unless ref $self; print $self->parse($_) for @_; } sub parse { my $self = shift; my ($text) = @_; my $tags = $self->allowed_tags; my ($before, $match) = ($text =~ m{ \A # start of string (.*?) # leading text ($before) ( # either... \<\!-- # the start of a comment | # or... $tags\< # a tag ) }xsm) or do { my @return = split /\|/, $text; $return[0] = encode_entities($return[0]); return @return; }; # strip $before from $text substr($text, 0, length($before)) = ''; # If the first thing that needed to be handled was a comment if ($match eq ' TEXT