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

Hi Monks,

I have what seems to me to be a straightforward task: to output the contents of an array of hashes into a DBF file via XBase.

I've been poring over the XBase Docs and FAQ and I can't seem to figure this out. The problem is that the object I should be creating with XBase, isn't recognized by XBase. Here's the code:

use XBase; ... my $new_table = XBase->create("name" => "$filename.$ext", "field_names" => @column_names, "field_types" => undef, "field_lengths" => undef, "field_decimals" => undef); my $i=0; foreach my $record (@records) { $new_table->set_record_hash($i,%$record); $i++; } $new_table->close;

The problem is, I get an error message: "Can't call method "set_record_hash" on an undefined value at /home/www/xina-cgi/libexec/xiexport.cgi line 264." Which I take to mean that it doesn't recognize $new_table as an XBase object.

So I thought that perhaps once I created the new table, I needed to define a new XBase object, and I tried this:

my $new_table = XBase->create("name" => "$filename.$ext", "field_names" => @column_names, "field_types" => undef, "field_lengths" => undef, "field_decimals" => undef); my $dbf = new XBase "$filename.$ext" or Xina::util::graceful_e +xit("xiexport","Problems with DBF file.","$filename.$ext:".XBase->err +str." from $remote_host"); my $i=0; foreach my $record (@records) { $dbf->set_record_hash($i,%$record); $i++; } $dbf->close;

Except now, I get the error: "Error opening file xxx.dbf: No such file or directory" - it doesn't create the file in the first place (I verified that by looking at the directory). But there is no error thrown on the create step. So I'm stumped. The directory is writeable, blah, blah, there are no file permission issues here.

Thanks for any pointers!

Michelle

Replies are listed 'Best First'.
Re: XBase File output
by Zaxo (Archbishop) on Feb 08, 2004 at 05:14 UTC

    I think you misunderstood guha's comments. You must supply references to arrays of meaningful values for "field_types", "field_lengths" and "field_decimals". That is what guha said.

    After Compline,
    Zaxo

Re: XBase File output
by guha (Priest) on Feb 08, 2004 at 00:19 UTC

    The undefs to types, lengths and decimals looks real suspicious. Also the name should be given without extension, XBase will default to .dbf, you will have to rename explicitely.

    Did you check

    XBase->errstr;
    HTH
      According to the Docs "If you set some value as undefined, create will make it into some reasonable default." So I figured I was OK.

      The XBase->errstr is what I posted ("Error opening file xxx.dbf: No such file or directory").

      I'll try using the name w/o an extension (I've used XBase before for reading files, and I used the extension there, so I figured I should use it here, too).

        Anyway the docs talks about references to list. You have given an array for the fieldnames argument.