The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SPOPS::Import::Object - Import SPOPS objects

SYNOPSIS

# Create the importer and read in the properties and data

my $importer = SPOPS::Import->new( 'object' )
                            ->data_from_file( 'mydata.dat' );

# Modify the 'name' field in every record

my $fields_h = $importer->fields_as_hashref;
my $name_idx = $fields_h->{name};
foreach my $data ( @{ $self->data } ) {
    $data->[ $name_idx ] =~ s/YourClass/MyClass/;
}

# Run the import and display the results

my $status = $importer->run;
foreach my $entry ( @{ $status } ) {
  if ( $entry->[0] ) { print "$entry->[1][0]: OK\n" }
  else               { print "$entry->[1][0]: FAIL ($entry->[2])\n" }
}

DESCRIPTION

This class implements simple data import for SPOPS objects using a serialized Perl data structure for the data storage.

For more information on SPOPS importing in general, see SPOPS::Manual::ImportExport and SPOPS::Import.

METHODS

fields_as_hashref()

Translate the field arrayref (returned by the fields() call) into a hashref of fieldname to position in data record. This is useful if you want to modify the data after they have been read in -- since the data are position- rather than name-indexed, you will need to map the name to the index.

So you you had:

my $fields = $importer->fields
print Dumper( $fields );
my $fields_h = $importer->fields_as_hashref;
print Dumper( $fields_h );

You might wind up with:

$VAR1 = [
        'first',
        'second',
        'third',
        'fourth'
        ];
$VAR1 = {
        'first' => 0,
        'fourth' => 3,
        'third' => 2,
        'second' => 1
        };

data_from_file( $filename )

Read the metadata and data from $filename. Runs assign_raw_data() to put the information into the object.

data_from_fh( $fh )

Read the metadata and data from the filehandle $fh. Runs assign_raw_data() to put the information into the object.

assign_raw_data( \@raw_data )

Assigns the raw data \@raw_data to the object. The first item should be metadata, and all remaining items are the data to be inserted.

The metadata should at least have the keys object_class and fields (or spops_class and field_order, respectively, for backward compatibility).

Other metadata you include is available through the extra_metadata property. These metadata might be for application-specific purposes.

After this is run the object should have available for inspection the following properties:

  • object_class

  • fields

  • data

BUGS

None known.

TO DO

Nothing known.

SEE ALSO

SPOPS::Manual::ImportExport

SPOPS::Import

SPOPS::Export::Object

COPYRIGHT

Copyright (c) 2001-2002 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>