{
    "content": [
        {
            "type": "text",
            "text": "# fields (man)\n\n## NAME\n\nfields - compile-time class fields\n\n## SYNOPSIS\n\n{\npackage Foo;\nuse fields qw(foo bar Fooprivate);\nsub new {\nmy Foo $self = shift;\nunless (ref $self) {\n$self = fields::new($self);\n$self->{Fooprivate} = \"this is Foo's secret\";\n}\n$self->{foo} = 10;\n$self->{bar} = 20;\nreturn $self;\n}\n}\nmy $var = Foo->new;\n$var->{foo} = 42;\n# this will generate a run-time error\n$var->{zap} = 42;\n# this will generate a compile-time error\nmy Foo $foo = Foo->new;\n$foo->{zap} = 24;\n# subclassing\n{\npackage Bar;\nuse base 'Foo';\nuse fields qw(baz Barprivate);        # not shared with Foo\nsub new {\nmy $class = shift;\nmy $self = fields::new($class);\n$self->SUPER::new();                # init base fields\n$self->{baz} = 10;                  # init own fields\n$self->{Barprivate} = \"this is Bar's secret\";\nreturn $self;\n}\n}\n\n## DESCRIPTION\n\nThe \"fields\" pragma enables compile-time and run-time verified class fields.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "fields",
        "section": "",
        "mode": "man",
        "summary": "fields - compile-time class fields",
        "synopsis": "{\npackage Foo;\nuse fields qw(foo bar Fooprivate);\nsub new {\nmy Foo $self = shift;\nunless (ref $self) {\n$self = fields::new($self);\n$self->{Fooprivate} = \"this is Foo's secret\";\n}\n$self->{foo} = 10;\n$self->{bar} = 20;\nreturn $self;\n}\n}\nmy $var = Foo->new;\n$var->{foo} = 42;\n# this will generate a run-time error\n$var->{zap} = 42;\n# this will generate a compile-time error\nmy Foo $foo = Foo->new;\n$foo->{zap} = 24;\n# subclassing\n{\npackage Bar;\nuse base 'Foo';\nuse fields qw(baz Barprivate);        # not shared with Foo\nsub new {\nmy $class = shift;\nmy $self = fields::new($class);\n$self->SUPER::new();                # init base fields\n$self->{baz} = 10;                  # init own fields\n$self->{Barprivate} = \"this is Bar's secret\";\nreturn $self;\n}\n}",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 40,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 71,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 5,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "fields - compile-time class fields\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "{\npackage Foo;\nuse fields qw(foo bar Fooprivate);\nsub new {\nmy Foo $self = shift;\nunless (ref $self) {\n$self = fields::new($self);\n$self->{Fooprivate} = \"this is Foo's secret\";\n}\n$self->{foo} = 10;\n$self->{bar} = 20;\nreturn $self;\n}\n}\n\nmy $var = Foo->new;\n$var->{foo} = 42;\n\n# this will generate a run-time error\n$var->{zap} = 42;\n\n# this will generate a compile-time error\nmy Foo $foo = Foo->new;\n$foo->{zap} = 24;\n\n# subclassing\n{\npackage Bar;\nuse base 'Foo';\nuse fields qw(baz Barprivate);        # not shared with Foo\nsub new {\nmy $class = shift;\nmy $self = fields::new($class);\n$self->SUPER::new();                # init base fields\n$self->{baz} = 10;                  # init own fields\n$self->{Barprivate} = \"this is Bar's secret\";\nreturn $self;\n}\n}\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "The \"fields\" pragma enables compile-time and run-time verified class fields.\n\nNOTE: The current implementation keeps the declared fields in the %FIELDS hash of the calling\npackage, but this may change in future versions.  Do not update the %FIELDS hash directly,\nbecause it must be created at compile-time for it to be fully useful, as is done by this\npragma.\n\nIf a typed lexical variable (\"my Class $var\") holding a reference is used to access a hash\nelement and a package with the same name as the type has declared class fields using this\npragma, then the hash key is verified at compile time.  If the variables are not typed,\naccess is only checked at run time.\n\nThe related \"base\" pragma will combine fields from base classes and any fields declared using\nthe \"fields\" pragma.  This enables field inheritance to work properly.  Inherited fields can\nbe overridden but will generate a warning if warnings are enabled.\n\nOnly valid for Perl 5.8.x and earlier: Field names that start with an underscore character\nare made private to the class and are not visible to subclasses.\n\nAlso, in Perl 5.8.x and earlier, this pragma uses pseudo-hashes, the effect being that you\ncan have objects with named fields which are as compact and as fast arrays to access, as long\nas the objects are accessed through properly typed variables.\n\nThe following functions are supported:\n\nnew fields::new() creates and blesses a hash comprised of the fields declared using the\n\"fields\" pragma into the specified class.  It is the recommended way to construct a\nfields-based object.\n\nThis makes it possible to write a constructor like this:\n\npackage Critter::Sounds;\nuse fields qw(cat dog bird);\n\nsub new {\nmy $self = shift;\n$self = fields::new($self) unless ref $self;\n$self->{cat} = 'meow';                      # scalar element\n@$self{'dog','bird'} = ('bark','tweet');    # slice\nreturn $self;\n}\n\nphash\nThis function only works in Perl 5.8.x and earlier.  Pseudo-hashes were removed from Perl\nas of 5.10.  Consider using restricted hashes or fields::new() instead (which itself uses\nrestricted hashes under 5.10+).  See Hash::Util.  Using fields::phash() under 5.10 or\nhigher will cause an error.\n\nfields::phash() can be used to create and initialize a plain (unblessed) pseudo-hash.\nThis function should always be used instead of creating pseudo-hashes directly.\n\nIf the first argument is a reference to an array, the pseudo-hash will be created with\nkeys from that array.  If a second argument is supplied, it must also be a reference to\nan array whose elements will be used as the values.  If the second array contains less\nelements than the first, the trailing elements of the pseudo-hash will not be\ninitialized.  This makes it particularly useful for creating a pseudo-hash from\nsubroutine arguments:\n\nsub dogtag {\nmy $tag = fields::phash([qw(name rank sernum)], [@]);\n}\n\nfields::phash() also accepts a list of key-value pairs that will be used to construct the\npseudo hash.  Examples:\n\nmy $tag = fields::phash(name => \"Joe\",\nrank => \"captain\",\nsernum => 42);\n\nmy $pseudohash = fields::phash(%args);\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "base, Hash::Util\n\n\n\nperl v5.34.0                                 2025-07-25                                fields(3perl)",
                "subsections": []
            }
        }
    }
}