in reply to XML::LibXML save variable data to a file

What exactly do you want to have appear in the output file - the XML, or the output of your print? For the former, see toFile in XML::LibXML::Document, so e.g. $doc->toFile("output.xml",1);. For the latter, there is some example code for opening and printing to files in perlintro.