NAME
SPOPS::Import - Factory and parent for importing SPOPS objects
SYNOPSIS
my $importer = SPOPS::Import->new( 'object' );
$importer->object_class( 'My::Object' );
$importer->fields( [ 'name', 'title', 'average' ] );
$importer->data( [ [ 'Steve', 'Carpenter', '160' ],
[ 'Jim', 'Engineer', '178' ],
[ 'Mario', 'Center', '201' ] ]);
$importer->run;
DESCRIPTION
This class is a factory class for creating importer objects. It is also the parent class for the importer objects.
METHODS
I/O
read_file( $filename )
Reads a file from $filename
, returning the content.
read_perl_file( $filename )
Reads a file from $filename
, then does an eval on the content to get back a Perl data structure. (Normal string eval
caveats apply.)
Returns the Perl data structure.
read_fh( $filehandle )
Reads all (or remaining) information from $filehandle
, returning the content.
raw_data_from_file( $filename )
Reads $filename
as a Perl data structure and does perliminary checks to ensure it can be used in an import.
raw_data_from_fh( $filehandle )
Reads $filehandle
as a Perl data structure and does perliminary checks to ensure it can be used in an import.
Properties
object_class
Class of the object to import
data
Data for this import. The implementation subclass specifies its format, but it is normally either an arrayref of arrayrefs or an arrayref of hashrefs.
extra_metadata
Placeholder for user-defined metadata.
Subclasses
Subclasses should override the following methods.
run()
Runs the import and, unless otherwise specified, returns an arrayref of status entries, one for each record it tried to import.
Each status entry is an arrayref formatted:
[ status (boolean), record, message ]
If the import for this record was successful, the first (0) entry will be true, the second (1) will be the object inserted (if possible, otherwise it is the data structure as if the record failed), and the third (2) entry will be undefined.
If the import for this record failed, the first (0) entry will be undefined, the second (1) will be the data structure we tried to insert, and the third (2) entry will contain an error message.
Whether the import succeeds or fails, the second entry will contain the record we tried to import. The record is an arrayref, and if you want to map the values to fields just ask the importer object for its fields:
my $field_name = $importer->fields->[1];
# On a success (if possible):
foreach my $item ( @{ $status } ) {
print "Value of $field_name: ", $item->[1]->$field_name(), "\n";
}
# On a failure (or success, if not possible):
foreach my $item ( @{ $status } ) {
print "Value of $field_name: $item->[1][1]\n";
}
BUGS
None known.
TO DO
Import XML
We currently export XML documents but we do not import them. It would be useful to do this.
SEE ALSO
COPYRIGHT
Copyright (c) 2001-2004 intes.net, inc.. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHORS
Chris Winters <chris@cwinters.com>