An example Perl manipulator

This Perl manipulator example adds a Record Number property to each record. The example uses the prepare, next_record, and finish methods.

First, the General tab shows the name of the component and that prepare, next_record, and finish methods are checked, which means that each method is defined in the Method Override editor rather than as an external file or class.



Clicking Edit for the prepare() method displays the Method Override editor, which contains the Perl code shown below. This code makes sure there is only one record source and initializes the record count to zero.

# Make sure there is exactly one record source configured
my @source_list = $this->record_sources;

if (scalar(@{ $this->record_sources }) != 1) {
   die("Perl Manipulator ", $this->name,
      " must have exactly one record source.");
}
# Keep the current record number in our context.
$this->context->{RECNO} = 0;

Clicking Edit for the next_record() method displays the Method Override editor which contains the Perl code shown below. This code counts the records processed and tags each record with a property value with that indicates the record number.

#Count this record.
++$this->context->{RECNO};
my $rec = $this->record_source(0)->next_record;

# Careful: $rec will be undef if there are no more records.
if ($rec) {
   my $pval = new EDF::PVal("Record Number", $this->context->{RECNO});
   $rec->add_pvals($pval);
}
return $rec;

Clicking Edit for the finish() method displays the Method Override editor, which contains the Perl code shown below. This code prints the number of records processed using the print_info method.

# Print the number of records processed.
EDF::print_info("Perl Manipulator ".
   $this->name, " processed ".
      $this->context->{RECNO}.
      " records.");