The Perl Toolchain Summit 2025 Needs You: You can help 🙏 Learn more

NAME

Data::Maker::Field::Code - A Data::Maker field class that generates its data based on a code reference. It was written specifically to allow for certain fields to be based on the value of some other field in the same record.

SYNOPSIS

my $maker = Data::Maker->new(
record_count => 10,
fields => [
{
name => 'firstname',
class => 'Data::Maker::Field::Person::FirstName'
},
{
name => 'lastname',
class => 'Data::Maker::Field::Person::LastName'
},
{
name => 'username',
class => 'Data::Maker::Field::Code',
args => {
code => sub {
my ($this, $maker) = @_;
my $first = $maker->in_progress('firstname');
my $lastname = $maker->in_progress('lastname');
my $username = lc( substr($first, 0, 1) . $lastname );
return $username;
}
}
},
]
);
while(my $record = $maker->next_record) {
print $record->username . "\n";
}

DESCRIPTION

Data::Maker::Field::Code takes a single argument, code, whose value must be a code reference that is run to obtain the value of this field.

This class can be used when you need the value of one field to be derived from the value of another field. This can be done using Data::Maker's in_progress method, as in the example above.