The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


SPOPS::Import::Object - Import SPOPS objects


 # Define a data file 'mydata.dat'
    { spops_class => 'OpenInteract2::Security',
      field_order => [ qw/ class object_id scope scope_id security_level / ],
      transform_default_to_id    => [ 'scope_id' ] },
    [ 'OpenInteract2::Action::Error', 0, 'w', 'world', 4 ],
    [ 'OpenInteract2::Action::Error', 0, 'g', 'site_admin_group', 8 ],
 # 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 ( @{ $importer->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" }


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.



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 = [
  $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






Copyright (c) 2001-2004, inc.. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Chris Winters <>