title: How can I write XML data to a file with Perl?
body: <p>Could you please correct my code below.</p>
  
  <pre><code>#!/usr/local/bin/perl
  
  open (MYFILE, '&gt;&gt;data.xml');
  print MYFILE "&lt;?xml version="1.0" encoding="UTF-8"?&gt;\n";
  close (MYFILE); 
  </code></pre>
  
  <p>Updated.</p>
  
  <pre><code>#!/usr/local/bin/perl
  open (MYFILE, '&gt;&gt;data.xml');
  print MYFILE '&lt;?xml version="1.0" encoding="UTF-8"?\&gt;'."\n";
  print MYFILE '&lt;?xml version="1.0" encoding="UTF-16"?\&gt;'."\n";
  close (MYFILE);
  </code></pre>
  
  <p>output:  working well now.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?\&gt;
  &lt;?xml version="1.0" encoding="UTF-16"?\&gt;
  </code></pre>
  
  <p>BUT.</p>
  
  <pre><code>#!/usr/local/bin/perl
  open (MYFILE, '&gt;&gt;data.xml');
  print MYFILE '&lt;?xml version="1.0" encoding="UTF-8"?\&gt;'.'\n';
  print MYFILE '&lt;?xml version="1.0" encoding="UTF-16"?\&gt;'.'\n';
  close (MYFILE);
  </code></pre>
  
  <p>Output:  # error format with \n</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?\&gt;\n&lt;?xml version="1.0" encoding="UTF-16"?\&gt;\n
  </code></pre>