Bitmask::Data - Handle unlimited length bitmasks in an easy and flexible

     # Create a simple bitmask class
     package MyBitmask;
     use base qw(Bitmask::Data);
        'value1' => '0b000000000000000001',
        'value2' => '0b000000000000000010',
        'value2' => '0b000000000000000100',
        'value4' => '0b000000000000001000',
        'value5' => '0b000000000000010000',
     ## Somewhere else in your code
     use MyBitmask;
     my $bm1 = MyBitmask->new('value1','value3');
     my $bm2 = MyBitmask->new('0b000000000000010010');
     my $bm3 = $bm1 | $bm2; 

    This package helps you dealing with bitmasks. First you need to subclass
    Bitmask::Data and set the bitmask values and length. (If you are only
    working with a single bitmask in a simple application you might also
    initialize the bitmask directly in the Bitmask::Data module).

    After the initialization you can create an arbitrary number of bitmask
    objects which can be accessed and manipulated with convenient methods
    and overloaded arithmetic and bit operators.

    Bitmask::Data does not store bitmasks as integers internally, but as
    strings conststing of \0 and \1, hence makinging unlimited length
    bitmasks possible (32-bit perl can handle integer bitmasks only up to 40

  Class Methods
    Set/Get the length of the bitmask. Do not change this value after the

    Bitmask length is unlimited.

    Default: 16

    Set/Get the default bitmask for empty Bitmask::Data objects.

    Default: undef

    If true value that disables warnings for lazy initialization. (Lazy
    initialization = call of init without bitmask bit values).

    Default: 0

        'value1', # will be 0b000001
        'value2', # will be 0b000010
        'value3'  # will be 0b000100

    If bitmask_lazyinit is 2 then bit values will be filled from left to
    right, otherwise from right to left

        'value1', # will be 0b100000
        'value2', # will be 0b010000
        'value3'  # will be 0b001000

    HASHREF of all bitmask items, with values as keys and bitmask as values.

        CLASS->init(LIST of VALUES);

    Initializes the bitmask class. You can supply a list of possible values.
    Optionally you can also specify the bits for the mask by adding bit
    values after the value.

            'value1' => 0b000001,
            'value2' => 0b000010,
            'value3' => 0b001000,
            'value4' => 0b010000,

    With "bitmask_lazyinit" enabled you can also skip the bitmask bit values


    Bits may be supplied as integers, strings or Math::BigInt objects (not

            'value1' => 0b000001,               # integer
            'value2' => 2,                      # integer
            'value3' => '0b000100'              # string starting with '0b'
            'value4' => '0B001000'              # string starting with '0B'
            'value5' => '\0\1\0\0\0\0'          # string consisting of \0 and \1
            'value6' => Math::BigInt->new("32") # Math::BigInt object

        my $bitmask_string = CLASS->int2bit(INTEGER);

    Helper method that turns an integer into the internal bitmask

        my $bitmask_string = CLASS->string2bit(STRING);

    Helper method that takes a string like '0B001010' or '0b010101' and
    turns it into the internal bitmask representation

        my $bitmask_string = CLASS->any2bitmask(ANYTHING);

    Helper method that tries to turn a data into the internal bitmask
    representation. This method can hanle

    *   any Bitmask::Data object

    *   Math::BigInt object

    *   a string matching on of the bitmask values

    *   a bitmask string consisting of \0 and \1 characters

    *   a bitmask string starting with '0b' or '0B' and containing only 0
        and 1

    *   an integer

        my $bitmask_string = CLASS->_parse_params(LIST)

    Helper method for parsing params passed to various methods.

  Overloaded operators
    Bitmask::Data uses overload by default.

    *   Numeric context

        Returns bitmask integer value (see integer method). For large
        bitmasks (> 40 bits) this will allways be a Math::BigInt object
        (hence using this method is not recommended).

    *   Scalar context

        Returns bitmask string representation (see string method)

    *   ==, eq, <=>, cmp

        Works like 'has_any'

    *   Smartmatch

        Works like has_any.

         $bm = new Somebitmask->new('v1','v2');
         $bm ~~ ['v1','v3'] # true, because 'v1' matches even if 'v3' is not set

    *   +, -

        Adds/Removes bits to/from the bitmask without changing the current
        object. The result is returned as a new Bitmask::Data object.

    *   -=, +=

        Adds/Removes bits to/from the current bitmask object.

    *   ~, ^, &, |

        Performs the bitwise operations without changing the current object.
        The result is returned as a new Bitmask::Data object.

    *   ^=, &=, |=

        Performs the bitwise operations on the current bitmask object.

        my $bm = MyBitmask->new();
        my $bm = MyBitmask->new('value1');
        my $bm = MyBitmask->new('0b00010000010000');
        my $bm = MyBitmask->new(124);
        my $bm = MyBitmask->new(0b00010000010000);
        my $bm = MyBitmask->new(0x2);
        my $bm = MyBitmask->new($another_bm_object);
        my $bm = MyBitmask->new("\0\1\0\0\1");
        my $bm = MyBitmask->new('value2', 'value3');
        my $bm = MyBitmask->new([32, 'value1', 0b00010000010000]);

    Create a new bitmask object. You can supply almost any combination of
    ARRAYREFS, bits, Bitmask::Data objects, Math::BigInt objects, bitmasks
    and values, even mix different types. See any2bitmask for details on
    possible formats.

        my $bm = MyBitmask->new_from_bitmask($bitmask_string);

    Create a new bitmask object from a bitmask string (as returned by many
    helper methods).

  Public Methods
        my $bm_new = $bm->clone();

    Clones an existing Bitmask::Data object and.


    This methpd resets the current bitmask and sets the supplied arguments.
    Takes the same arguments as "new".

    Returns the object.


    Removes the given values/bits from the bitmask. Takes the same arguments
    as "new".

    Returns the object.


    Adds the given values/bits to the bitmask. Takes the same arguments as

    Returns the object.


    Resets the bitmask to the default (or empty) bitmask.

    Returns the object.


    Sets all defined bits in the bitmask.

    Returns the object.


    Negates/Inverts the bitmask

    Returns the object.

        my @values = $bm->list();
        my $values = $bm->list();

    In list context, this returns a list of the set values in scalar
    context, this returns an array reference to the list of values.

        my $length = $bm->length();

    Number of set bitmask values.

        my $value = $bm->first()

    Returns the first set value. The order is determined by the bit value.

        my $integer = $bm->integer();

    Returns the bitmask as an integer. For bitmasks with a length > 40 this
    will always be a Math::BigInt object.

        my $string = $bm->string();

    Returns the bitmask as a string of 0 and 1.

        my $string = $bm->bitmask();

    Returns the bitmask in the internal representation: A string of \0 and

    This method can be used for database searches in conjunction with
    SQL::Abstract an POSTGRESQL (SQL::Abstract is used by DBIx::Class for
    generating searches). The search will find all database rows with
    bitmask that have at least the given values set. (use the "sql" method
    for an exact match)

    Example how to use sqlfilter with SQL::Abstract:

        my($stmt, @bind) = $sql->select(

    Example how to use sqlfilter with DBIx::Class:

        my $list = $resultset->search(

    Works like "sqlfilter_all" but checks for any bit matching

    Returns the bitmask as a quoted string as needed by PostgreSQL:


        if ($bm->has_all(PARAMS)) {
            # Do something

    Checks if all requestes bits/values are set and returns true or false.
    This method takes the same arguments as "new".

        if ($bm->has_exact(PARAMS)) {
            # Do something

    Checks if the set bits/values excactly match the supplied bits/values
    and returns true or false. This method takes the same arguments as

        if ($bm->has_any(PARAMS)) {
            # Do something

    Checks if at least one set value/bit matches the supplied bits/values
    and returns true or false. This method takes the same arguments as

    Since Bitmask::Data is very liberal with input data you cannot use
    numbers as bitmask values. (It would think that you are supplying an
    integer bitmask and not a value)

    Bitmask::Data adds a considerable processing overhead to bitmask
    manipulations. If you either don't need the extra comfort or use
    bitmasks with less that 40 bits that you should consider using just the
    perl built in bit operators on simple integer values.

    Bitmask::Data was designed to be subclassed.

        package MyBitmask;
        use parent qw(Bitmask::Data);
        __PACKAGE__->bitmask_length(20); # Default length is 16
            'value1' => 0b000000000000000001,
            'value2' => 0x2,
            'value2' => 4,
            'value4', # lazy initlialization
            'value5', # lazy initlialization

    This module comes with support for POSTGRESQL databases (patches for
    other database vendors are welcome).

    First you need to create the correct column types:

        CREATE TABLE bitmaskexample ( 
            id integer DEFAULT nextval('pkey_seq'::regclass) NOT NULL,
            bitmask bit(14),
            otherfields character varying

    The length of the bitmask field must match "CLASS->bitmask_length".

    This module provides three convenient methods to work with databases:

    *   sqlfilter_all: Search for matching bitmasks

    *   sqlfilter_any: Search for bitmasks with matching bits

    *   string: Print the bitmask string as used by the database

    If you are working with l<DBIx::Class> you might also install de- and
    inflators for Bitmask::Data objects:

            inflate => sub {
                my $value = shift;
                return MyBitmask->new($value);
            deflate => sub {
                my $value = shift;
                undef $value 
                    unless ref($value) && $value->isa('MyBitmask');
                $value //= MyBitmask->new();
                return $value->string;

    Please report any bugs or feature requests to
    "", or through the web interface at
    <>. I will
    be notified and then you'll automatically be notified of the progress on
    your report as I make changes.

        Klaus Ita
        koki [at]

        Maroš Kollár
        CPAN ID: MAROS
        maros [at]

    This module was originally written by Klaus Ita (Koki) for Revdev
    <>, a nice litte software company I (Maros) run with
    Koki and Domm (<>).

    Bitmask::Data is Copyright (c) 2008 Klaus Ita, Maroš Kollár -

    This program is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

    The full text of the license can be found in the LICENSE file included
    with this module.