Badger::Config(3pm)
π Badger::Config β the vibe β¨
okay bestie, so this is Badger::Config, the configuration module from the Badger family (it's a 3pm page, perl vibes only). Basically: it holds your config data and lets you grab it back out with cute little methods. Let's get into it π«Ά
πΈ SYNOPSIS πΈ
use Badger::Config;
my $config = Badger::Config->new(
user => {
name => {
given => 'Arthur',
family => 'Dent',
},
email => [
'arthur@dent.org',
'dent@heart-of-gold.com',
],
},
planet => {
name => 'Earth',
description => 'Mostly Harmless',
},
);
# fetch top-level data item - these both do the same thing
my $user = $config->user; # shortcut method
my $user = $config->get('user'); # generic get() method
# fetch nested data item - these all do the same thing
print $config->get('user', 'name', 'given'); # Arthur
print $config->get('user.name.family'); # Dent
print $config->get('user/email/0'); # arthur@dent.org
print $config->get('user email 1'); # dent@heart-of-gold.com
no cap, the shortcut method and the generic get() method are literally doing the same thing. pick your fave π
π¦ DESCRIPTION π¦
So the tea is: this started as a quick hack, a lil placeholder for the Badger::Config module. A config object is currently little more than a blessed hash with an AUTOLOAD method that lets you get/set items via methods. humble beginnings β¨
Update: it's been improved a little since the above was written. It's still incomplete, but it's being worked on. it's giving WIP, and we respect the journey π
π METHODS π
β¨ new()
Constructor method, aka the glow-up moment where you make a brand new Badger::Config object. You can hand it configuration data as the data named parameter:
my $config = Badger::Config->new(
data => {
name => 'Arthur Dent',
email => 'arthur@dent.org',
},
);
The items parameter is how you list the names of other valid configuration values that this object supports:
my $config = Badger::Config->new(
data => {
name => 'Arthur Dent',
email => 'arthur@dent.org',
},
items => 'planet friends',
);
Any data items you defined in either data or items can be accessed via methods:
print $config->name; # Arthur Dent
print $config->email; # arthur@dent.org
print $config->planet || 'Earth'; # Earth
As a shortcut (because we love convenience π ) you can also specify configuration data direct to the method:
my $config = Badger::Config->new(
name => 'Arthur Dent',
email => 'arthur@dent.org',
);
β οΈ You should avoid this usage if there is any possibility that your configuration data might contain thedataoritemsitems. fr fr, that's a collision waiting to happen π
π get($name)
Method to retrieve a value from the configuration:
my $name = $config->get('name');
It can also fetch nested data, which is where it gets iconic. You can specify each element as a separate argument, or as a string delimited with any non-word characters. So for example, given this configuration data:
my $config = Badger::Config->new(
user => {
name => {
given => 'Arthur',
family => 'Dent',
},
email => [
'arthur@dent.org',
'dent@heart-of-gold.com',
],
},
);
You can then access data items using any of the following syntax:
print $config->get('user', 'name', 'given'); # Arthur
print $config->get('user.name.family'); # Dent
print $config->get('user/email/0'); # arthur@dent.org
print $config->get('user email 1'); # dent@heart-of-gold.com
And not just list and hash array items β get() will also call subroutine references and object methods. Behold this somewhat contrived example, it's giving main character energy:
# a trivial object class
package Example;
use base 'Badger::Base';
sub wibble {
return 'wobble';
}
package main;
# a config with a function that returns a hash containing an object
my $config = Badger::Config->new(
function => sub {
return {
object => Example->new(),
}
}
);
print $config->get('function.object.wibble'); # wobble
she ate, honestly π₯
π set($name,$value)
Method to store a value in the configuration:
$config->set( friend => 'Ford Prefect' );
$config->set( friends => ['Ford Prefect','Trillian','Marvin'] );
At present this does not allow you to set nested data items in the way that the get() method does. so no nesting here bestie, it's a one-level situation for now π
π§ INTERNAL METHODS π§
π can_configure($name)
Internal method used to generate accessor methods on demand. This is installed using the auto_can hook in Badger::Class. iykyk, you probably don't need to touch this one directly π
β AUTHOR β
Andy Wardley <http://wardley.org/> β the icon behind it all π
π COPYRIGHT π
Copyright (C) 2001-2009 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay, open source rights β¨
and that's the whole page bestie π«Ά this was Badger::Config, page 3pm, from perl v5.36.0, dated 2023-08-28. periodt π
πΈ
π¦ where this page comes from
- debian trixie: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms