Dave Rolsky


Antispam::Toolkit::Role::BerkeleyDB - A role for classes which store spam check data in a BerkeleyDB file


version 0.08


  package MyDBD;

  use Moose;

  with 'Antispam::Toolkit::BerkeleyDB';

  sub _store_value {
      my $class = shift;
      my $db    = shift;
      my $value = shift;



This role provides most of what's needed in order to store spam-checking data in a BerkeleyDB file. The only method you must implement in your class is the $class->_store_value() method.

Typically, this will be a database containing things like bad ip addresses or usernames.


This role provides the following attributes:


This is a Path::Class::File object representing the path on disk for the BerkeleyDB file. This attribute also accepts a string, which is coerced to a file object.


The name of the database. This can be any non-empty string. It is intended for use in things like logs, so that you know exactly which database matched a particular value.

By default, the name will contain the database file's basename and it's mtime as an ISO8601 datetime, something like "bad-ip.db - 2010-11-16T10:31:03".


This attribute contains the BerkeleyDB object for the database. It cannot be set in the constructor, and is always lazily built.


This role requires one method:


This role provides the following methods:


This will build the BerkeleyDB object for the database.


This creates a default name for the object.

$class->build( ... )

This is a class method that can be used to construct a new BerkeleyDB file from a data source.

It accepts the following arguments:

  • file

    This should be a file containing data to be imported into the database. By default, this should be a file which lists one value per line. You can provide your own $class->_extract_data_from_file() to handle different data formats.

  • database

    The path to the BerkeleyDB file that will be created or updated.

    If you're using multiple BerkeleyDB files for different types of data, you probably should put each one in a separate directory, because the BerkeleyDB library creates identically named log files for each database file.

  • update

    By default, if the database parameter points to an existing BerkeleyDB file, it will be emptied completely and rebuilt from scratch. If this parameter is true, it will simply add new data and leave the old data in place.

$class->_extract_data_from_file( $file, $db )

This method takes a data file and adds that data to the BerkeleyDB file. By default, this expects that the file contains one value per line, so it chomps each line and stores that value.

Internally, it calls $class->_store_value() to actually store the value.

$class->_store_value( $db, $value )

This method will be called as a class method. The method is passed a BerkeleyDB object and value to store in the database.

By default, it just stores the literal value. You can replace this method if you want to do something different, like handle wildcard values.


This method looks up a value to see if it is stored in the database. By default, it expects the value to match a key stored in the database.


This role does the Antispam::Toolkit::Role::Database role. It provides an implementation of $db->match_value() method, but you can write your own if necessary.


See Antispam::Toolkit for bug reporting details.


Dave Rolsky <autarch@urth.org>


This software is Copyright (c) 2011 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)