Home > Archive > PERL Beginners > April 2007 > XML::Writer creates a file but fails to be recognized
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
XML::Writer creates a file but fails to be recognized
|
|
| Dave Adams 2007-04-26, 6:58 pm |
| When generating a file with XML::Writer the script certainly builds the file
but when I go to test for it, it fails. Does anyone have a reason why? How
do I create a file that I can use in the rest of my script?
use XML::Writer;
use IO::File;
my $output = new IO::File(">test.xml");
my $writer = new XML::Writer(OUTPUT => $output);
$writer->startTag("greeting","class" => "simple");
$writer->characters("Hello, world!");
$writer->endTag("greeting");
$writer->end();
$output->close();
#Test to make sure this file exist before preceding
if (! -r $output) {
print ("ERROR: can't read /$output XML file.");
}
| |
| Rob Dixon 2007-04-26, 6:58 pm |
| Dave Adams wrote:
>
> When generating a file with XML::Writer the script certainly builds the
> file but when I go to test for it, it fails. Does anyone have a reason why?
> How do I create a file that I can use in the rest of my script?
>
> use XML::Writer;
> use IO::File;
> my $output = new IO::File(">test.xml");
> my $writer = new XML::Writer(OUTPUT => $output);
> $writer->startTag("greeting","class" => "simple");
> $writer->characters("Hello, world!");
> $writer->endTag("greeting");
> $writer->end();
> $output->close();
>
> #Test to make sure this file exist before preceding
> if (! -r $output) {
> print ("ERROR: can't read /$output XML file.");
> }
Please, always
use strict;
use warnings;
at the start of your programs. That will find a lot of simple problems.
You're testing whether your file handle $output is opened to a read-permitted
file. First of all you opened it write-only so you won't be able to read from
the handle even if you have read permissions. Secondly you've closed the handle,
so it's not referring to a file at all any more.
Just open the file for read, checking any errors you get. If you don't
have read permission then the open will fail:
open my $in, 'test.xml' or die "ERROR: can't read XML file: $!";
while (<$in> ) {
print;
}
HTH,
Rob
|
|
|
|
|