Badger...s::Vars(3pm)

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

πŸ’– NAME β€” Badger::Class::Vars ✨

okay so bestie, this is Badger::Class::Vars, and it's a class module for defining package variables. that's it, that's the job. it's a perl thing, a 3pm manual page, and it's lowkey iconic πŸ’…


🌸 SYNOPSIS 🌸

here's the whole vibe, fr fr:

package My::Module;

# simple pre-declaration of variables
use Badger::Class::Vars '$FOO @BAR %BAZ';

# pre-declaration with values
use Badger::Class::Vars
    '$FOO' => 10,
    '@BAR' => [20, 30, 40],
    '%BAZ' => { x => 100, y => 200 };

# via Badger::Class
use Badger::Class
    vars => '$FOO @BAR %BAZ';

# via Badger::Class with values
use Badger::Class
    vars => {
        '$FOO' => 10,
        '@BAR' => [20, 30, 40],
        '%BAZ' => { x => 100, y => 200 },
    };

look at that. four whole ways to do the thing. main character energy for real πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so this module lets you pre-declare package variables and, if you want, define values for them too. you can use it straight up, or you can roll in through the vars export hook in Badger::Class. either way, you're eating πŸ“

# using the module directly
use Badger::Class::Vars
    '$FOO @BAR %BAZ';

# using it via Badger::Class
use Badger::Class
    vars => '$FOO @BAR %BAZ';

in the simple case it works just like the vars.pm module does, pre-declaring the variables you name. same energy, different font ✨

but here's the plot twist: unlike vars.pm, this method will only define scalar, list and hash package variables β€” like $SOMETHING, @SOMETHING or %SOMETHING. that's the whole menu, no substitutions πŸ™ˆ

wanna define subroutines/methods instead? then go use the Badger::Class::Methods module, or the methods import hook, or the methods() method in Badger::Class. and if you wanna define a glob reference? bestie you are already operating in Wizard Mode and you don't need our help πŸ§™β™€οΈπŸ’€

okay now. if you don't give a leading sigil (that's "$", "@" or "%"), it defaults to $ and makes you a scalar variable. no sigil, no problem:

use Badger::Class
    vars => 'FOO BAR BAZ';      # declares $FOO, $BAR and $BAZ

you can also hand it a reference to a hash array to define values for your variables. it's giving organised:

use Badger::Class
    vars => {                           # Equivalent code:
        '$FOO' => 42,                   #   our $FOO = 25
        '@WIZ' => [100, 200, 300],      #   our @WIZ = (100, 200, 300)
        '%WOZ' => {ping => 'pong'},     #   our %QOZ = (ping => 'pong')
    };
not us side-eyeing that 25 and that %QOZ up there πŸ’€ but every character of the original stays, periodt. we don't edit, we just vibe.

scalar package variables can be assigned any scalar value or a reference to some other data type. and again, that leading "$" is optional on the variable names. note the difference in the equivalent code tho β€” this time you end up with scalar variables and references exclusively, no cap:

use Badger::Class
    vars => {                           # Equivalent code:
        FOO => 42,                      #   our $FOO = 42
        BAR => [100, 200, 300],         #   our $BAR = [100, 200, 300]
        BAZ => {ping => 'pong'},        #   our $BAZ = {ping => 'pong'}
        HAI => sub {                    #   our $HAI = sub { ... }
            'Hello ' . (shift || 'World')
        },
    };

and you can assign any kind of data to a package list variable too 🫢 if it's not already a list reference, then the value gets treated as a single item list. it's giving "one item is a list, actually":

use Badger::Class
    vars => {                           # Equivalent code:
        '@FOO' => 42,                   #   our @FOO = (42)
    };

πŸŽ€ METHODS πŸŽ€

vars($target,$vars)

this method defines variable in the $target package. it's usually called automatically when the module gets loaded via use, so most of the time you don't even have to think about it. it just shows up for you πŸ’…

the $vars can be a single text string of whitespace delimited symbols, or a reference to a list of individual symbols. either way, the variables get declared but undefined.

# single string
Badger::Class::Vars->vars(
    'My::Package',
    '$FOO, @BAR, %BAZ'
);

# list reference
Badger::Class::Vars->vars(
    'My::Package',
    ['$FOO', '@BAR', '%BAZ']
);

and if you want to provide values for the variables? use a reference to a hash array, bestie:

# hash reference
Badger::Class::Vars->vars(
    'My::Package',
    {
        '$FOO'  => 10,
        '@BAR' => [20, 30, 40],
        '%BAZ' => { x => 100, y => 200 },
    }
);

slay. that's the method. ✨


πŸ‘‘ AUTHOR πŸ‘‘

Andy Wardley <http://wardley.org/>


Copyright (C) 2008-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.


and that's a wrap, bestie πŸŽ€ this little page came to you from perl v5.36.0, dated 2023-08-28, under the name Badger...s::Vars(3pm). go declare some variables. you're gonna ate. πŸŒˆπŸ’–

πŸ“¦ where this page comes from