|
NAMEType::Tiny::Bitfield - bitfield/bitflag type constraints SYNOPSISUsing 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') );
}
}
STATUSThis module is covered by the Type-Tiny stability policy. DESCRIPTIONBitfield type constraints. This package inherits from Type::Tiny; see that for most documentation. Major differences are listed below: Attributes
MethodsThis 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->GREEN" would return 2. Other methods it provides:
ExportsType::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:
Multiple bitfield types can be exported at once: use Type::Tiny::Enum (
LedSet => { RED => 1, GREEN => 2, BLUE => 4 },
LedPattern => { FLASHING => 1 },
);
OverloadingIt 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. BUGSPlease report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>. SEE ALSOType::Tiny::Manual. Type::Tiny. AUTHORToby Inkster <tobyink@cpan.org>. COPYRIGHT AND LICENCEThis 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 WARRANTIESTHIS 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.
|