tags:

views:

345

answers:

8

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
+4  A: 
print MYFILE qq{<?xml version="1.0" encoding="UTF-8"?>\n};

Your problem is that you had nested double quotes; using qq{} to delimit the string will solve this issue.

ennuikiller
If you use single quotes, the \n won't work.
Paul Tomblin
@Paul I stand corrected. Thanks!
ennuikiller
@ennuikiller: You do realize you can use `qq`, I hope.
Sinan Ünür
+3  A: 

I would recommend to use:

use XML::Simple;
Jay Zeng
@Jay: Could you post a simple example using `XML::Simple`?
Zaid
@Zaid: There are numerous tutorials online, here is one of the good ones: http://www.ibm.com/developerworks/xml/library/x-xmlperl1.html
Jay Zeng
XML::Writer is another pretty straightforward one
plusplus
+1  A: 

Here is some code of mine for printing an XML file:

open(XML, ">$xmlfile");
print XML (<<EOF);
<?xml version="1.0" encoding="UTF-8"?>
<gpx
 version="1.1"
 creator="Navaid Waypoint Generator - http://navaid.com/GPX/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.topografix.com/GPX/1/1"
 xmlns:navaid="http://navaid.com/GPX/NAVAID/1/0"
 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd
 http://navaid.com/GPX/NAVAID/1/0 http://navaid.com/GPX/NAVAID/1/0"&gt;
 <metadata>
    <author>
        <name>Paul Tomblin</name>
        <email id="ptomblin" domain="xcski.com"/>
        <link href="http://blog.xcski.com/"/&gt;
    </author>
    <link href="http://navaid.com/GPX/"/&gt;
</metadata>
EOF
Paul Tomblin
Why the downvote? It illustrates that you can avoid the quoting problems by using a here document. If you think I'm somehow pimping my *free* software site, I can take out the references to it if you like, but it's not like I'm going to reach the expected audience (pilots) by posting it on a programming site.
Paul Tomblin
a good way too. thanks.
Nano HE
Downvote by other guest. I just run the script above. it is working now. i also agree with that it is a good way to avoid the quoting problems... :-)
Nano HE
+4  A: 

The problem is you have unescaped quotes in the string. Either escape the quotes using the backslash or surround your print string with qq{}:

print MYFILE qq{<?xml version="1.0" encoding="UTF-8"?>\n};
--or--
print MYFILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
tracer0xa
+1  A: 

Always, always, ALWAYS check the value returned from open, e.g.,

open (MYFILE, '>>data.xml')
  or die "$0: open: $!";

Note the important bits in the error message:

  1. the name of the program that failed, $0
  2. what it was trying to do, open
  3. why it failed, $!

Without a newline at the end of the string passed to die, it appends the file and line number where your program died.

Greg Bacon
+5  A: 

Always turn on warnings and strictures, so you find out earlier what went wrong, and get more details why:

#!/usr/local/bin/perl
use strict;
use warnings;

Always use the lexical-variabled, three-argument form of open (there's a big discussion why over here), and always check the return value (it will return an error if something went wrong, and put the reason why in the $! variable (see under $! at perldoc perlvar). Also, die will print the line number of where the program quit if you don't end your string with a \n (more at perldoc -f die).

open my $file, '>>', 'data.xml' or die "Can't open file: $!";

And use double-quotes around the \n so that it is printed properly:

print $file '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
print $file '<?xml version="1.0" encoding="UTF-16"?>' . "\n";
close $file;
Ether
+1 for recommendations to help in the future.
Chris Lutz
+1 for details description.
Nano HE
A: 

Several additional points of advice:

daotoad
@Peter, thanks for the edit.
daotoad
+1  A: 

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:

open my($fh), '>:utf8', 'data.xml' or die "Could not open file: $!";

print $fh qq{<?xml version="1.0" encoding="UTF-8">\n};
brian d foy