If XML::Twig could detect that the request is before the root AND the request is for a comment or a processing instruction, then just call the _add_cpi_outside_of_root function. Or if the request is after the root AND the request is for a PI, then do the same. (Comments after root allowed? I'm not sure.)
It makes sense, I'll do just that (yes comments are allowed after the root, it's in the first production rule in the spec, the Misc after the element).