Badger::Class Metaprogramming
package NewSkool::Constants; use Badger::Class version => 2.718, constant => { MESSAGE => 'Hello World!', VOLUME => 11, TRUE => 1, FALSE => 0, }, exports => { all => 'MESSAGE', any => 'VOLUME', tags => { truth => 'TRUE FALSE' }, hooks => { ... } fail => sub { ... } };
- No
use strict
oruse warnings
required (thx Moose!) - No need to subclass from
Badger::Exporter
explicitly.
Thus Spake Andy:
...and here's what it looks like using Badger::Class
. We pass it three
parameters when we use
it (these are what we call import/export hooks). The version
tells it to simply set a $VERSION
package variable. The constant
tells it
to define some constants (just like saying use constant
). The export
hook is forward to the Badger::Exporter
exports()
method that we were
just looking at. There's no need to use strict
or use warnings
thanks
to a trick we borrowed from Moose
. Also note that we don't need to
explicitly subclass from Badger::Exporter
because that is implicit in the
fact that we declared some exports.
The obvious feature is that Badger::Class
effectively helps you to avoid
writing some of that messy "boilerplate" code that many modules typically
start with. On a deeper level, it's a shift towards a more declarative style
of programming. For example, we simply declare what we export and make the
nuts and bolts of that process Somebody Else's Problem™. On an aesthetic
level, it's grouping related functionality into one place and presenting it in
a consistent fashion. The whole block becomes instantly recognisable as "all
that startup crap" and can easily be skimmed (i.e. ignored). If your
editor/IDE is smart enough, then you can collapse this down so that the whole
thing only uses up a single line of your precious screen space. Furthermore,
the indenting gives a visual clue to the structure of the data (and again,
smart editors can collapse each of these blocks in expanding tree-view style).
Skimpy++