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

Config::MVP::Assembler - multivalue-property config-loading state machine

VERSION

version 2.200013

DESCRIPTION

First, you should probably read the example of using Config::MVP. If you already know how it works, keep going.

Config::MVP::Assembler is a helper for constructing a Config::MVP::Sequence object. It's a very simple state machine that lets you signal what kind of events you've encountered while reading configuration.

PERL VERSION

This module should work on any version of perl still receiving updates from the Perl 5 Porters. This means it should work on any version of perl released in the last two to three years. (That is, if the most recently released version is v5.40, then this module should work on both v5.40 and v5.38.)

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.

ATTRIBUTES

sequence_class

This attribute stores the name of the class to be used for the assembler's sequence. It defaults to Config::MVP::Sequence.

section_class

This attribute stores the name of the class to be used for sections created by the assembler. It defaults to Config::MVP::Section.

sequence

This is the sequence that the assembler is assembling. It defaults to a new instance of the assembler's sequence_class.

METHODS

begin_section

$assembler->begin_section($package_moniker, $name);

$assembler->begin_section($package_moniker);

$assembler->begin_section( \$package );

This method tells the assembler that it should begin work on a new section with the given identifier. If it is already working on a section, an error will be raised. See "change_section" for a method to begin a new section, ending the current one if needed.

The package moniker is expanded by the "expand_package" method. The name, if not given, defaults to the package moniker. These data are used to create a new section and the section is added to the end of the sequence. If the package argument is a reference, it is used as the literal value for the package, and no expansion is performed. If it is a reference to undef, a section with no package is created.

end_section

$assembler->end_section;

This ends the current section. If there is no current section, an exception is raised.

change_section

$assembler->change_section($package_moniker, $name);

$assembler->change_section($package_moniker);

This method calls begin_section, first calling end_section if needed.

add_value

$assembler->add_value( $name => $value );

This method tells the assembler that it has encountered a named value and should add it to the current section. If there is no current section, an exception is raised. (If this is not the first time we've seen the name in the section and it's not a multivalue property, the section class will raise an exception on its own.)

expand_package

This method is passed a short identifier for a package and is expected to return the full name of the module to load and package to interrogate. By default it simply returns the name it was passed, meaning that package names must be given whole to the change_section method.

current_section

This returns the section object onto which the assembler is currently adding values. If no section has yet been created, this method will return false.

TYPICAL USE

my $assembler = Config::MVP::Assembler->new;

# Maybe you want a starting section:
my $starting_section = $assembler->section_class->new({ name => '_' });
$assembler->sequence->add_section($section_starting);

# We'll add some values, which will go to the starting section:
$assembler->add_value(x => 10);
$assembler->add_value(y => 20);

# Change to a new section...
$assembler->change_section($moniker);

# ...and add values to that section.
$assembler->add_value(x => 100);
$assembler->add_value(y => 200);

The code above creates an assembler and populates it step by step. In the end, to get values, you could do something like this:

my @output;

for my $section ($assembler->sequence->sections) {
  push @output, [ $section->name, $section->package, $section->payload ];
}

When changing sections, the given section "moniker" is used for the new section name. The result of passing that moniker to the assembler's "expand_package" method is used as the section's package name. (By default, this method does nothing.) The new section's multivalue_args and aliases are determined by calling the mvp_multivalue_args and mvp_aliases methods on the package.

AUTHOR

Ricardo Signes <cpan@semiotic.systems>

COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Ricardo Signes.

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