my $subtree = $dom->find($whatever); $subtree->delete if $substree; #### #pseudo-code - does not run!!! $dom->find($whatever)?->delete;