#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::LibXML; for my $other_title ( '', 'old value', ) { my $xml = qq( $other_title ); my $dom = 'XML::LibXML'->load_xml(string => $xml); my ($other_title) = $dom->findnodes('//file/body/group/unit/alt-title/otherTitle'); if ($other_title->findnodes('text()')) { $other_title->removeChildNodes; } $other_title->appendText('new value'); print $dom; } #### ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,