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

Hi,

I have a simple PERL script thats called using a HTML form.

All it does is takes a string then opens a file and writes XML content to it.

Here is a small code sample:

use DBI; use HTML::Entities (); use IO; use XML::Writer; use Date::Language; use CGI; my $query = new CGI; print $query->header(); $atom_id = $query->param('atom_id'); my $lang = Date::Language->new('English'); # Set up DB parameters my $dbh = DBI->connect ("DBI:mysql:database=sbsContacts;host=localhost +", "root", "cobweb123", {RaiseError => 1, PrintError => 0}); # Outer loop - get all scheme IDs where flag is set $sbs_query = $dbh->prepare("SELECT * FROM sbs_contacts WHERE for_sbs = + 'Y' AND atom_id = ?"); $sbs_query->execute($atom_id); # Create the file where all the contact XML will be printed my $output = new IO::File(">$atom_id.xml"); #PROBLEM HERE! my $writer = new XML::Writer(OUTPUT => $output, DATA_MODE => 'true', D +ATA_INDENT => 2);

In the browser the code fails on the last line but its ok on command line. Can anyone help.

Craig

20040705 Edit by Corion: Put code in code tags

20040706 Edit by castaway: Changed title from 'XML::WRITER'

Replies are listed 'Best First'.
Re: XML::Writer writes file from the command line but not via a CGI
by Happy-the-monk (Canon) on Jul 05, 2004 at 09:08 UTC

    In the browser the code fails on the last line but its ok on command line.

    The user on the command line often has different permissions than the user running the web server.
    Try testing on the command line as the user running the webserver.

    Also test for success when opening the connection to the database and when opening the file:

    my $dbh = DBI->connect( ... ) or die $DBI::errstr;
    my $output = new IO::File( ... ) or die $!

    Cheers, Sören

      Permissions!!!
      Darn it.


      Thank you soooo much!