Config...:Slicer(3pm)

πŸ“¦
version 0.303 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ Config::MVP::Slicer πŸ’…

okay bestie, welcome to Config::MVP::Slicer β€” that's a Perl module page from the 3pm manual section, and it is genuinely iconic. version 0.303, no cap. πŸŽ€

It's a Perl thing. It's from the User Contributed Perl Documentation. And it does one very specific, very slay job, which we're about to get into. πŸ¦‹


🌸 NAME 🌸

Config::MVP::Slicer β€” Extract embedded plugin config from parent config πŸ’–

like. it slices. it dices. it pulls your plugin's little config out of the big config. that's the whole vibe.


🌈 VERSION 🌈

version 0.303 β€” the current one, bestie ✨


πŸŽ€ SYNOPSIS πŸŽ€

here's how you actually use it, copy this energy:

my $slicer = Config::MVP::Slicer->new({
  config => $parent->config,
});

# extract a hashref from the parent config without modifying the plugin
my $plugin_config = $slicer->slice($plugin);

# from plugin bundles:
my $plugin_spec = ['Name', 'Package::Name', {default => 'config'}];
# update the hashref
$slicer->merge($plugin_spec);

# with object instances:
my $plugger = App::Plugin::Plugger->new({some => 'config'});
# update 'rw' attributes
$slicer->merge($plugger);

periodt. πŸ’…


🧚 DESCRIPTION 🧚

so the whole point: this module lets you pull embedded configurations for other plugins out of a bigger (parent) configuration. it's giving "extract the specific thing out of the big thing." ✨

the classic example where this eats is plugin bundles β€” peep Config::MVP::Assembler::WithBundles for that.

here's the situation. a bundle loads a bunch of other plugins with a default config that works most of the time. but sometimes you want to customize the config for one of those plugins, and you do not want to have to rip the plugin out of the bundle and re-specify it separately. so annoying. πŸ™ˆ

# mvp config file
[@MyBundle]
Other::Plugin.setting = new value

and now? you can accept customizations to plugins into your bundle config, and separate them out using this module. ate. πŸ‘‘


πŸ‘‘ ATTRIBUTES πŸ‘‘

config

this is the main/parent configuration hashref that contains embedded plugin configurations. the big one. the mothership. πŸ’œ

match_name

this is a coderef that determines if a configuration line matches a plugin's name.

you can customize it by passing an alternate subroutine reference to the constructor. πŸ’…

the sub will receive two arguments:

  • The plugin name portion of the configuration line
  • The name of the plugin being worked on (provided to "slice", for instance).

the default returns true if the current plugin name matches the name from the config line, regardless of any leading "@Bundle/" prefixes in the plugin name (because that's a common convention for bundles).

obviously if the "@Bundle/" prefix is specified in the configuration, then it is required to be there for the default sub to match β€” but multiple other "@Bundle/" prefixes will be allowed before it. noted. 🫢

# configuration line: "Foo.attr = value"

$slicer->match_name("Foo", "Foo");            # true
$slicer->match_name("Foo", "@Bar/Foo");       # true
$slicer->match_name("Foo", "Bar");            # false

# configuration line: "@Bar/Foo.attr = value"

$slicer->match_name("@Bar/Foo", "Foo");           # false
$slicer->match_name("@Bar/Foo", "@Bar/Foo");      # true
$slicer->match_name("@Bar/Foo", "@Baz/@Bar/Foo"); # true
$slicer->match_name("@Bar/Foo", "@Baz/Foo");      # false

subclasses can define "_build_match_name" (which should return a "sub") to overwrite the default. πŸ¦‹

match_package

this works like "match_name" except that the configuration line is compared to the plugin's package (class).

the default returns true if the two values are equal and false otherwise.

if you want to match by package rather than name, and you expand packages with (for example) a string prefix, you may need to set this to something like:

match_package => sub { rewrite_prefix($_[0]) eq $_[1] }

subclasses can define "_build_match_package" (which should return a "sub") to overwrite the default. ✨

prefix

regular expression that should match at the beginning of a key, before the module name and attribute:

# prefix => 'dynamic\.'
# { 'dynamic.Module::Name.attr' => 'value' }

this can be a string or a compiled regular expression ("qr//").

the default is no prefix (empty string ''). πŸ’–

separator

a regular expression that will capture the package name in $1 and the attribute name in $2.

the default ("(.+?)\.(.+?)") separates plugin name from attribute name with a dot:

'Module::Name.attribute'
'-Plugin.attr'
NOTE: The regexp should not be anchored, since "separator_regexp" uses it as the middle piece of a larger regexp (to add "prefix" and the possible array bracket suffix). Also beware of using a regexp that greedily matches the array bracket suffix, as that can confuse things as well. 😭 watch out bestie.

⭐ METHODS ⭐

separator_regexp

returns a compiled regular expression ("qr//") combining "prefix", "separator", and the possible trailing array specification ("\[.*?\]"). 🌈

slice

$slicer->slice($plugin);

return a hashref of the config arguments for the plugin determined by $plugin.

this is a slice of the "config" attribute appropriate for the plugin passed to the method.

starting with a config hashref of:

{
  'APlug:attr1'   => 'value1',
  'APlug:second'  => '2nd',
  'OtherPlug:attr => '0'
}

passing a plugin instance of 'APlug' (or an arrayref of "['APlug', 'Full::Package::APlug', {}]") would return:

{
  'attr1'   => 'value1',
  'second'  => '2nd'
}

so clean. so satisfying. 🧚

merge

$slicer->merge($plugin, \%opts);

get the config slice (see "slice"), then attempt to merge it into the plugin.

if $plugin is an arrayref the hashref will be modified. if it's an object, its attributes should be writable ('rw'). πŸ’…

this will append to array references if it was specified as an array, or if a preexisting value is an arrayref.

returns the modified $plugin for convenience. cute.

possible options:

  • "slice" β€” A hashref like that returned from "slice". If not present, "slice" will be called.

plugin_info

$slicer->plugin_info($plugin);

used by other methods to normalize the information about a plugin. returns a list of "($name, $package, \%config)".

if $plugin is an arrayref it will simply dereference it. this can be useful for processing the results of plugin bundles. πŸŽ€

if $plugin is an instance of a plugin that has a "plugin_name" method, it will construct the list from that method, "ref", and the instance itself.


πŸ”₯ CONFIGURATION SYNTAX πŸ”₯

often configurations come from an "ini" file and look like this:

[PluginName]
option = value

this gets converted to a hashref:

PluginName->new({ option => 'value' });

to embed configuration for other plugins:

[@BigBundle]
bundle_option = value
Bundled::Plugin.option = other value

the simple 'bundle_option' attribute is for @BigBundle, and the bundle can slice out the "Bundled::Plugin" configuration and merge it in to that plugin's configuration. slay. πŸ’œ

prefixes can be used (see "prefix"). in this example the prefix is set as "plug.".

[@Foo]
plug.Bundled::Plugin.attr = value

due to limitations of this dynamic passing of unknown options (otherwise known as a hack πŸ’€), values that are arrays cannot be declared ahead of time by the bundle. you can help out by specifying that an attribute should be an array:

[@Bar]
Baz.quux[0] = part 1
Baz.quux[1] = part 2

this is required because each line will end up in a hashref:

{ "quux[0]" => "part 1", "quxx[1]" => "part 2" }

the subscripts inside the brackets are used for sorting but otherwise ignored. the "slice" method will sort the keys (alphabetically) to produce:

{ quux => ["part 1", "part 2"] }

for simplicity the keys are sorted alphabetically, because "quux[1.9]" and "quux[1.10]" probably won't sort the way you intended anyway, so just keep things simple:

[@Bundle]
Plug.attr[0] = part 1
Plug.attr[1] = part 2
Plug.other[09] = part 1
Plug.other[10] = part 2
Plug.alpha[a] = part 1
Plug.alpha[b] = part 2
Plug.alpha[bc] = part 3
Plug.single[] = subscript not required; only used for sorting

iconic. keep it simple bestie. 🌸


🫢 SUPPORT 🫢

Perldoc

you can find documentation for this module with the perldoc command.

perldoc Config::MVP::Slicer

Websites

the following websites have more information about this module, and may be of help to you. as always, in addition to those websites please use your favorite search engine to discover more resources. πŸ”

  • MetaCPAN

A modern, open-source CPAN search engine, useful to view POD in HTML format.

<http://metacpan.org/release/Config-MVP-Slicer>

Bugs / Feature Requests

please report any bugs or feature requests by email to "bug-config-mvp-slicer at rt.cpan.org", or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=Config-MVP-Slicer>. you will be automatically notified of any progress on the request by the system. πŸ’Œ

Source Code

<https://github.com/rwstauner/Config-MVP-Slicer>

git clone https://github.com/rwstauner/Config-MVP-Slicer.git

πŸ’– AUTHOR πŸ’–

Randy Stauner <rwstauner@cpan.org> β€” the main character, honestly πŸ‘‘


this software is copyright (c) 2011 by Randy Stauner.

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


okay that's the whole page, you're fully caught up bestie ✨ and just so you know: this was generated by perl v5.34.0 and dated 2022-06-30, which is the footer energy, and honestly? iconic of it. πŸ’…πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from