aerris has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am very new to perl and am trying to use perl to create an xml file and let the user specify the filename. Here is my code:
#!/usr/bin/perl
use Fcntl; #The Module

print "Series:\n";
$series = <>;
print "Filename:\n";
$filename = <>;


open (XMLFILE,">c:/Perl_Tests/temp.xml");
printf XMLFILE '<?xml version="1.0" encoding="UTF-8"?>\n';
printf XMLFILE '<XML TAGS>';
printf XMLFILE '<SERIES>'.$series.'</SERIES>';
printf XMLFILE '<ETC>'.$etc.'</ETC>';
printf XMLFILE '<ETC>'.$etc.'</ETC>';
printf XMLFILE '</XMLTAGS>';
close(XMLFILE);

I want the use to specify the filename (instead of temp.xml) but I can't get the value of $filename into that path: eg
open (XMLFILE,">c:/Perl_Tests/".$filename.".xml");
or something like that. Can anyone help? Also... I want to put carriage returns after each line of XML so when the outputted file is view in a text editor it is easier to read... I can create the file and enter the XML into it - i just cant specify the name of the file during the program execution... i have to rename it aftewards. Thanks!

Replies are listed 'Best First'.
Re: Specifying filename
by Corion (Patriarch) on Jun 12, 2008 at 12:10 UTC

    When you read a line from user input, the newline character is still at the end of the line. Some filesystems have problems with creating a filename that contains a newline character. So you should remove that character using the chomp function:

    chomp $filename;

    Also, it is a very good practice to check whether your open() calls succeed or not, and to output a relevant error message:

    open XMLFILE, '>', $filename) or die "Couldn't create '$filename': $!";
Re: Specifying filename
by apl (Monsignor) on Jun 12, 2008 at 12:01 UTC
    What does
    can't get the value of $filename into that path ... or something like that
    mean?

    Change

    open (XMLFILE,">c:/Perl_Tests/".$filename.".xml");
    to
    my $path = ">c:/Perl_Tests/".$filename.".xml"; open (XMLFILE, $path) or die "Opening $path : $!";
    Then you can see what error ($!) caused the open to fail (as well as the path that was used).

    You should also add use strict; use warnings at the top of your program.

Re: Specifying filename
by pseudomonas (Monk) on Jun 12, 2008 at 12:11 UTC
    Obligatorily: use strict; use warnings;
    You might want to chomp $filename to remove the newline at the end of the user-supplied data.
    Linebreaks in your XML: the \n probably isn't doing what you want inside single-quotes. Try doublequotes?
    Given that the text you're using already has double-quotes in it, you can get the same effect with the qq function: print qq{This is a string with "quotes" in it\n};
Re: Specifying filename
by toolic (Bishop) on Jun 12, 2008 at 12:58 UTC
    In addition to the advice already provided, consider replacing your sequence of print statements by a much simpler and less verbose HERE doc:
    open my $fh, '>', $filename or die "Can not open file $filename: $!"; print $fh <<"EOF"; <?xml version="1.0" encoding="UTF-8"?> <XML TAGS> <SERIES>$series</SERIES> <ETC>$etc</ETC> <ETC>$etc</ETC> </XMLTAGS> EOF close $fh;

    That being said, I second the recommendation to consider using a CPAN module for creating the XML file.

Re: Specifying filename
by jeepj (Scribe) on Jun 12, 2008 at 12:12 UTC
    In addition, you could also use something like XML::Simple to build your XML file, it would be easier.

      ... and it takes care of escaping reserved chars.

      But, pleeease, try XML::Writer instead of XML::Simple.

      XML::Simple is so "simple" that in the last month I answered to no less than 4 posts about it, and the issue was always about the incorrect use of the not-so-simple-and-not-at-all-obvious options of that module.

      Really, I'ld have nothing against XML::Simple if only it had a different name :) Something in the line of XML::Config wold be nice, for it is (explicitly) optimized for managing XML config files, not generic XML.

      Careful with that hash Eugene.

        Alternatives to XML::Writer are XML::Generator and my own XML::API module.
Re: Specifying filename
by Anonymous Monk on Jun 15, 2008 at 18:15 UTC
    There is no need to take the variable out of the quoted area: open (XMLFILE,">c:/Perl_Tests/$filename.xml"); should work fine.