If you want to stick with Twig, you can do it like this:
#!usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $xml = XML::Twig->new(twig_handlers => {
'p' => sub {
if (!$_->first_child()) { $_->set_content('#EMPTY') }
},
},
pretty_print => 'indented',
empty_tags => 'normal'
);
$xml->parsefile('file.xml');
$xml->print;
Basically you have to manually check if the element contains nothing, then set it to be an empty element.