Type::Tiny::Bitfield(3) User Contributed Perl Documentation Type::Tiny::Bitfield(3)

# [NAME](#NAME)

Type::Tiny::Bitfield - bitfield/bitflag type constraints

# [SYNOPSIS](#SYNOPSIS)

Using Type::Tiny::Bitfield's export feature:

```
  package LightSource {
    use Moo;
    
    use Type::Tiny::Bitfield LedSet => {
      RED   => 1,
      GREEN => 2,
      BLUE  => 4,
    };
    
    has leds => ( is => 'ro', isa => LedSet, default => 0, coerce => 1 );
    
    sub new_red {
      my $class = shift;
      return $class->new( leds => LEDSET_RED );
    }
    
    sub new_green {
      my $class = shift;
      return $class->new( leds => LEDSET_GREEN );
    }
    
    sub new_yellow {
      my $class = shift;
      return $class->new( leds => LEDSET_RED | LEDSET_GREEN );
    }
  }
```

Using Type::Tiny::Bitfield's object-oriented interface:

```
  package LightSource {
    use Moo;
    use Type::Tiny::Bitfield;
    
    my $LedSet = Type::Tiny::Bitfield->new(
      name   => 'LedSet',
      values => {
        RED   => 1,
        GREEN => 2,
        BLUE  => 4,
      },
      coercion => 1,
    );
    
    has leds => ( is => 'ro', isa => $LedSet, default => 0, coerce => 1 );
    
    sub new_red {
      my $class = shift;
      return $class->new( leds => $LedSet->RED );
    }
    
    sub new_green {
      my $class = shift;
      return $class->new( leds => $LedSet->GREEN );
    }
    
    sub new_yellow {
      my $class = shift;
      return $class->new( leds => $LedSet->coerce('red|green') );
    }
  }
```

# [STATUS](#STATUS)

This module is covered by the Type-Tiny stability policy.

# [DESCRIPTION](#DESCRIPTION)

Bitfield type constraints.

This package inherits from Type::Tiny; see that for most documentation. Major differences are listed below:

## [Attributes](#Attributes)

"values"

Hashref of bits allowed in the bitfield. Keys must be UPPER\_SNAKE\_CASE strings. Values must be positive integers which are powers of two. The same number cannot be used multiple times.

"constraint"

Unlike Type::Tiny, you *cannot* pass a constraint coderef to the constructor. Instead rely on the default.

"inlined"

Unlike Type::Tiny, you *cannot* pass an inlining coderef to the constructor. Instead rely on the default.

"parent"

Parent is always **Types::Common::Numeric::PositiveOrZeroInt**, and cannot be passed to the constructor.

"coercion"

If "coercion =&gt; 1" is passed to the constructor, the type will have an automatic coercion from **Str**. Types built by the "import" method will always have "coercion =&gt; 1".

In the SYNOPSIS example, the coercion from **Str** will accept strings like:

```
  "RED"
  "red"
  "Red Green"
  "Red+Blue"
  "blue | GREEN"
  "LEDSET_RED + LeDsEt_green"
    
```

## [Methods](#Methods)

This class uses "AUTOLOAD" to allow the names of each bit in the bitfield to be used as methods. These method names will always be UPPER\_SNAKE\_CASE.

For example, in the synopsis, "LedSet-&gt;GREEN" would return 2.

Other methods it provides:

[from\_string( $str )](#from_string%28)

Provides the standard coercion from a string, even if this type constraint doesn't have a coercion.

[to\_string( $int )](#to_string%28)

Does the reverse coercion.

[constant\_names()](#constant_names%28%29)

This is a convenience to allow for:

```
  use base 'Exporter::Tiny';
  push our @EXPORT_OK, LineStyle->constant_names;
    
```

## [Exports](#Exports)

Type::Tiny::Bitfield can be used as an exporter.

```
  use Type::Tiny::Bitfield LedSet => {
    RED    => 1,
    GREEN  => 2,
    BLUE   => 4,
  };
```

This will export the following functions into your namespace:

"LedSet"

[is\_LedSet( $value )](#is_LedSet%28)

[assert\_LedSet( $value )](#assert_LedSet%28)

[to\_LedSet( $string )](#to_LedSet%28)

[LedSet\_to\_Str( $value )](#LedSet_to_Str%28)

"LEDSET\_RED"

"LEDSET\_GREEN"

"LEDSET\_BLUE"

Multiple bitfield types can be exported at once:

```
  use Type::Tiny::Enum (
    LedSet     => { RED => 1, GREEN => 2, BLUE => 4 },
    LedPattern => { FLASHING => 1 },
  );
```

## [Overloading](#Overloading)

It is possible to combine two Bitfield types using the "+" operator.

```
  use Type::Tiny::Enum (
    LedSet     => { RED => 1, GREEN => 2, BLUE => 4 },
    LedPattern => { FLASHING => 8 },
  );
  
  has leds => (
    is      => 'ro',
    isa     => LedSet + LedPattern,
    default => 0,
    coerce  => 1
  );
```

This will allow values like "11" (LEDSET\_RED|LEDSET\_GREEN|LEDPATTERN\_FLASHING).

An exception will be thrown if any of the names in the two types being combined conflict.

# [BUGS](#BUGS)

Please report any bugs to &lt;https://github.com/tobyink/p5-type-tiny/issues&gt;.

# [SEE ALSO](#SEE_ALSO)

Type::Tiny::Manual.

Type::Tiny.

# [AUTHOR](#AUTHOR)

Toby Inkster &lt;tobyink@cpan.org&gt;.

# [COPYRIGHT AND LICENCE](#COPYRIGHT_AND_LICENCE)

This software is copyright (c) 2023-2025 by Toby Inkster.

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

# [DISCLAIMER OF WARRANTIES](#DISCLAIMER_OF_WARRANTIES)

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

2026-10-25 perl v5.44.0

---

Document: Type::Tiny::Bitfield(3pm) from perl-type-tiny 2.010001-r4 (Chainguard OS)
Source: https://github.com/tobyink/p5-type-tiny
