I have a script which basically (among other things) I want to read the contents of some XML files into a table in MySQL. I'm using DBI.pm to interact with MySQL. Everything else is working fine: I'm able to read the files, parse them a bit for other things, but at the end of the script, when I try to reopen the files to copy their total contents to MySQL, the contents aren't saved. Below is the sub-routine which I wrote to handle this part:

sub xmlfiles_to_mysql { my $xmlfile_contents; while(my ($file_id,$xml_file) = each(%xml_files) ) { open(XML_FILE, "./xml_files/$xml_file"); while (<XML_FILE>) { chomp; $xmlfile_contents .= $_; } my $sql_stmnt = "REPLACE INTO xml_files (file_id, xml_file) VALUES(?,?)"; my $sth = $dbh->prepare($sql_stmnt); $sth->execute($file_id,$xmlfile_contents); $sth->finish(); close XML_FILE; } }

The hash %xml_files is set up outside the sub-routine and seems to work alright. The table in MySQL have the names of the xml filse in the first column. They have NULL for the xml_file column. That column is a BLOB column.

I'm getting this error message for each file read:

readline() on closed filehandle XML_FILE at xml_processing.pl line 275.

Obviously the file is closing before it's finished. But I don't see how. Any thoughts on what I'm doing wrong or in the wrong order? I know it's probably simple, but I just don't see it. Thanks in advance for any help.

-Spenser

That's Spenser, with an "s" like the detective.


In reply to Copying Files to MySQL by Spenser

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.