{
    "mode": "perldoc",
    "parameter": "Type::Tiny::Bitfield",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3ABitfield/json",
    "generated": "2026-10-04T06:56:29Z",
    "synopsis": "Using Type::Tiny::Bitfield's export feature:\npackage LightSource {\nuse Moo;\nuse Type::Tiny::Bitfield LedSet => {\nRED   => 1,\nGREEN => 2,\nBLUE  => 4,\n};\nhas leds => ( is => 'ro', isa => LedSet, default => 0, coerce => 1 );\nsub newred {\nmy $class = shift;\nreturn $class->new( leds => LEDSETRED );\n}\nsub newgreen {\nmy $class = shift;\nreturn $class->new( leds => LEDSETGREEN );\n}\nsub newyellow {\nmy $class = shift;\nreturn $class->new( leds => LEDSETRED | LEDSETGREEN );\n}\n}\nUsing Type::Tiny::Bitfield's object-oriented interface:\npackage LightSource {\nuse Moo;\nuse Type::Tiny::Bitfield;\nmy $LedSet = Type::Tiny::Bitfield->new(\nname   => 'LedSet',\nvalues => {\nRED   => 1,\nGREEN => 2,\nBLUE  => 4,\n},\ncoercion => 1,\n);\nhas leds => ( is => 'ro', isa => $LedSet, default => 0, coerce => 1 );\nsub newred {\nmy $class = shift;\nreturn $class->new( leds => $LedSet->RED );\n}\nsub newgreen {\nmy $class = shift;\nreturn $class->new( leds => $LedSet->GREEN );\n}\nsub newyellow {\nmy $class = shift;\nreturn $class->new( leds => $LedSet->coerce('red|green') );\n}\n}",
    "sections": {
        "NAME": {
            "content": "Type::Tiny::Bitfield - bitfield/bitflag type constraints\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "Using Type::Tiny::Bitfield's export feature:\n\npackage LightSource {\nuse Moo;\n\nuse Type::Tiny::Bitfield LedSet => {\nRED   => 1,\nGREEN => 2,\nBLUE  => 4,\n};\n\nhas leds => ( is => 'ro', isa => LedSet, default => 0, coerce => 1 );\n\nsub newred {\nmy $class = shift;\nreturn $class->new( leds => LEDSETRED );\n}\n\nsub newgreen {\nmy $class = shift;\nreturn $class->new( leds => LEDSETGREEN );\n}\n\nsub newyellow {\nmy $class = shift;\nreturn $class->new( leds => LEDSETRED | LEDSETGREEN );\n}\n}\n\nUsing Type::Tiny::Bitfield's object-oriented interface:\n\npackage LightSource {\nuse Moo;\nuse Type::Tiny::Bitfield;\n\nmy $LedSet = Type::Tiny::Bitfield->new(\nname   => 'LedSet',\nvalues => {\nRED   => 1,\nGREEN => 2,\nBLUE  => 4,\n},\ncoercion => 1,\n);\n\nhas leds => ( is => 'ro', isa => $LedSet, default => 0, coerce => 1 );\n\nsub newred {\nmy $class = shift;\nreturn $class->new( leds => $LedSet->RED );\n}\n\nsub newgreen {\nmy $class = shift;\nreturn $class->new( leds => $LedSet->GREEN );\n}\n\nsub newyellow {\nmy $class = shift;\nreturn $class->new( leds => $LedSet->coerce('red|green') );\n}\n}\n",
            "subsections": []
        },
        "STATUS": {
            "content": "This module is covered by the Type-Tiny stability policy.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Bitfield type constraints.\n\nThis package inherits from Type::Tiny; see that for most documentation. Major differences are\nlisted below:\n",
            "subsections": [
                {
                    "name": "Attributes",
                    "content": "\"values\"\nHashref of bits allowed in the bitfield. Keys must be UPPERSNAKECASE strings. Values must\nbe positive integers which are powers of two. The same number cannot be used multiple times.\n\n\"constraint\"\nUnlike Type::Tiny, you *cannot* pass a constraint coderef to the constructor. Instead rely\non the default.\n\n\"inlined\"\nUnlike Type::Tiny, you *cannot* pass an inlining coderef to the constructor. Instead rely on\nthe default.\n\n\"parent\"\nParent is always Types::Common::Numeric::PositiveOrZeroInt, and cannot be passed to the\nconstructor.\n\n\"coercion\"\nIf \"coercion => 1\" is passed to the constructor, the type will have an automatic coercion\nfrom Str. Types built by the \"import\" method will always have \"coercion => 1\".\n\nIn the SYNOPSIS example, the coercion from Str will accept strings like:\n\n\"RED\"\n\"red\"\n\"Red Green\"\n\"Red+Blue\"\n\"blue | GREEN\"\n\"LEDSETRED + LeDsEtgreen\"\n"
                },
                {
                    "name": "Methods",
                    "content": "This class uses \"AUTOLOAD\" to allow the names of each bit in the bitfield to be used as methods.\nThese method names will always be UPPERSNAKECASE.\n\nFor example, in the synopsis, \"LedSet->GREEN\" would return 2.\n\nOther methods it provides:\n"
                },
                {
                    "name": "from_string",
                    "content": "Provides the standard coercion from a string, even if this type constraint doesn't have a\ncoercion.\n"
                },
                {
                    "name": "to_string",
                    "content": "Does the reverse coercion.\n"
                },
                {
                    "name": "constant_names",
                    "content": "This is a convenience to allow for:\n\nuse base 'Exporter::Tiny';\npush our @EXPORTOK, LineStyle->constantnames;\n"
                },
                {
                    "name": "Exports",
                    "content": "Type::Tiny::Bitfield can be used as an exporter.\n\nuse Type::Tiny::Bitfield LedSet => {\nRED    => 1,\nGREEN  => 2,\nBLUE   => 4,\n};\n\nThis will export the following functions into your namespace:\n\n\"LedSet\""
                },
                {
                    "name": "is_LedSet",
                    "content": ""
                },
                {
                    "name": "assert_LedSet",
                    "content": ""
                },
                {
                    "name": "to_LedSet",
                    "content": "LedSettoStr( $value )\n\"LEDSETRED\"\n\"LEDSETGREEN\"\n\"LEDSETBLUE\"\n\nMultiple bitfield types can be exported at once:\n\nuse Type::Tiny::Enum (\nLedSet     => { RED => 1, GREEN => 2, BLUE => 4 },\nLedPattern => { FLASHING => 1 },\n);\n"
                },
                {
                    "name": "Overloading",
                    "content": "It is possible to combine two Bitfield types using the \"+\" operator.\n\nuse Type::Tiny::Enum (\nLedSet     => { RED => 1, GREEN => 2, BLUE => 4 },\nLedPattern => { FLASHING => 8 },\n);\n\nhas leds => (\nis      => 'ro',\nisa     => LedSet + LedPattern,\ndefault => 0,\ncoerce  => 1\n);\n\nThis will allow values like \"11\" (LEDSETRED|LEDSETGREEN|LEDPATTERNFLASHING).\n\nAn exception will be thrown if any of the names in the two types being combined conflict.\n"
                }
            ]
        },
        "BUGS": {
            "content": "Please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Type::Tiny::Manual.\n\nType::Tiny.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Toby Inkster <tobyink@cpan.org>.\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENCE": {
            "content": "This software is copyright (c) 2023 by Toby Inkster.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        },
        "DISCLAIMER OF WARRANTIES": {
            "content": "THIS PACKAGE IS PROVIDED \"AS IS\" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\nWITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.\n",
            "subsections": []
        }
    },
    "summary": "Type::Tiny::Bitfield - bitfield/bitflag type constraints",
    "flags": [],
    "examples": [],
    "see_also": []
}