Could you please correct my code below.
#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE "<?xml version="1.0" encoding="UTF-8"?>\n";
close (MYFILE);
Updated.
#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE '<?xml version="1.0" encoding="UTF-8"?\>'."\n";
print MYFILE '<?xml version="1.0" encoding="UTF-16"?\>'."\n";
close (MYFILE);
output: working well now.
<?xml version="1.0" encoding="UTF-8"?\>
<?xml version="1.0" encoding="UTF-16"?\>
BUT.
#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE '<?xml version="1.0" encoding="UTF-8"?\>'.'\n';
print MYFILE '<?xml version="1.0" encoding="UTF-16"?\>'.'\n';
close (MYFILE);
Output: # error format with \n
<?xml version="1.0" encoding="UTF-8"?\>\n<?xml version="1.0" encoding="UTF-16"?\>\n
Several additional points of advice:
For writing complex, structured files, use a library like XML::Simple or one of the many, many others available on CPAN.
If you are writing many very similar files, a templating module may be more appropriate.
Take Ether's advice re:
open
,strict
andwarnings
. It will save you time debugging.If you are going to claim that a file has a particular encoding (UTF-8 or UTF-16), you probably should emit actual Unicode text to the file. There is more info in perldoc -f open.
If you want to write UTF-8 data to a file (as you say in your XML declaration, open the file with a UTF-8 encoding: