1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nano HE
  • 9,109
  • 31
  • 97
  • 137
  • I am newbie. A lot of good suggestion inputed!. I would like to try one by one. thank you all. – Nano HE Dec 17 '09 at 03:25
  • 1
    Like I said in a comment below, if you put \n in single quotes, it will print out two characters rather than a newline. – Paul Tomblin Dec 17 '09 at 03:41
  • If you are a newbie I would also recommed always to do 'use strict'. Put this line just after the line containing "#!". – sateesh Dec 17 '09 at 03:44
  • 1
    You do realize you are opening `data.xml` in append mode and then appending to it something that should be the first line of that file, **right**? – Sinan Ünür Dec 17 '09 at 03:45
  • @Sinan: It's a simple test script only. YES. I would like to run my perl script at append mode. Convert my source TXT file to destination XML file. – Nano HE Dec 17 '09 at 04:29

8 Answers8

6

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";
akciom
  • 566
  • 2
  • 4
5
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
5

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;
Community
  • 1
  • 1
Ether
  • 53,118
  • 13
  • 86
  • 159
3

I would recommend to use:

use XML::Simple;
Jay Zeng
  • 1,423
  • 10
  • 10
1

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">
 <metadata>
    <author>
        <name>Paul Tomblin</name>
        <email id="ptomblin" domain="xcski.com"/>
        <link href="http://blog.xcski.com/"/>
    </author>
    <link href="http://navaid.com/GPX/"/>
</metadata>
EOF
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 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 Dec 17 '09 at 03:40
  • 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 Dec 17 '09 at 03:44
1

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
  • 134,834
  • 32
  • 188
  • 245
1

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};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brian d foy
  • 129,424
  • 31
  • 207
  • 592
0

Several additional points of advice:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
daotoad
  • 26,689
  • 7
  • 59
  • 100