Badger::Config(3pm)

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

πŸ’– 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 the data or items items. 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 (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