More Badger::Class Hooks
package Your::Forager; use Badger::Class base => 'Badger::Base', utils => 'blessed params min max', constants => 'ARRAY CRLF DELIMITER', codec => 'storable+base64', filesystem => 'File'; sub hoard { my ($self, $filename, $data) = @_; File($filename)->write( encode($data) ); } sub forage { my ($self, $filename) = @_; decode( File($filename)->text ); }
Thus Spake Andy:
Here's another ever-so-slightly contrived example showing off some other
hooks. The utils hook does the same thing as use Badger::Utils in
importing any of the various utilities it defines or can load from other
modules. The constants (note plural) imports constants from
Badger::Constants. Note that we're not actually using any of these
utils or constants in the code snippets... they're just for show. However
we are using the next two items. The codec option is delegated to the
Badger::Codecs module. This constructs a composite codec which pipes
data through Storable and then MIME::Base64 all in one go. It exports
a codec constant subroutine which returns a codec object (codec->encode()
and codec->decode()) and encode() and decode() subroutines which work
in the traditional functional style. We're only using one codec, so this works
just fine. If we were using several separate codecs then we would have specified
a hash array so that we can name them (e.g. { codec1 => 'storable+base64', ... }).
The final filesystem option is delegated to Badger::Filesystem. It does
the same thing as writing use Badger::Filesystem 'File' in importing a
File() subroutine for constructing Badger::Filesystem::File objects.
This module is an example of a simple serialisation object which has a method
(hoard()) for serialising some data and storing it in a file. We create
a File() object from the $filename and then call its write() method,
passing the serialised data returned from our encode() function. The
forage() method fetches it back out by loading the file text and passing
it into the decode() method.