{"id":20315249,"url":"https://github.com/sysread/slot","last_synced_at":"2026-05-22T16:34:30.289Z","repository":{"id":56839797,"uuid":"127424507","full_name":"sysread/slot","owner":"sysread","description":"Fast, simple, compile-time class declaration for Perl","archived":false,"fork":false,"pushed_at":"2020-02-10T19:12:27.000Z","size":99,"stargazers_count":0,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-11T07:26:29.466Z","etag":null,"topics":["class","object","oo","perl","slot"],"latest_commit_sha":null,"homepage":null,"language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sysread.png","metadata":{"files":{"readme":"README.pod","changelog":"Changes","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-30T12:14:08.000Z","updated_at":"2020-02-10T19:12:25.000Z","dependencies_parsed_at":"2022-08-28T23:30:54.181Z","dependency_job_id":null,"html_url":"https://github.com/sysread/slot","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/sysread/slot","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysread%2Fslot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysread%2Fslot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysread%2Fslot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysread%2Fslot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sysread","download_url":"https://codeload.github.com/sysread/slot/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysread%2Fslot/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33354036,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-21T12:23:38.849Z","status":"online","status_checked_at":"2026-05-22T02:00:06.671Z","response_time":265,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["class","object","oo","perl","slot"],"created_at":"2024-11-14T18:18:30.955Z","updated_at":"2026-05-22T16:34:30.256Z","avatar_url":"https://github.com/sysread.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"=pod\n\n=encoding UTF-8\n\n=head1 NAME\n\nClass::Slot - Simple, efficient, comple-time class declaration\n\n=head1 VERSION\n\nversion 0.09\n\n=head1 SYNOPSIS\n\n  package Point;\n\n  use Class::Slot;\n  use Types::Standard -types;\n\n  slot x =\u003e Int, rw =\u003e 1, req =\u003e 1;\n  slot y =\u003e Int, rw =\u003e 1, req =\u003e 1;\n  slot z =\u003e Int, rw =\u003e 1, def =\u003e 0;\n\n  1;\n\n  my $p = Point-\u003enew(x =\u003e 10, y =\u003e 20);\n  $p-\u003ex(30); # x is set to 30\n  $p-\u003ey;     # 20\n  $p-\u003ez;     # 0\n\n=head1 DESCRIPTION\n\nSimilar to the L\u003cfields\u003e pragma, C\u003cslot\u003e declares individual fields in a\nclass, building a constructor and slot accessor methods.\n\nAlthough not nearly as full-featured as L\u003cother|Moose\u003e L\u003csolutions|Moo\u003e,\nC\u003cClass::Slot\u003e is light-weight, fast, works with basic Perl objects, and\nimposes no dependencies outside of the Perl core distribution. Currently, only\nthe unit tests require non-core packages.\n\nC\u003cClass::Slot\u003e is intended for use with Perl's bare metal objects. It provides\na simple mechanism for building accessor and constructor code at compile time.\n\nIt does I\u003cnot\u003e provide inheritance; that is done by setting C\u003c@ISA\u003e or via the\nC\u003cbase\u003e or C\u003cparent\u003e pragmas.\n\nIt does I\u003cnot\u003e provide method wrappers; that is done with the C\u003cSUPER\u003e\npseudo-class.\n\nIt I\u003cdoes\u003e build a constructor method, C\u003cnew\u003e, with support for default and\nrequired slots as keyword arguments and type validation of caller-supplied\nvalues.\n\nIt I\u003cdoes\u003e build accesor methods (reader or combined reader/writer, using the\nslot's name) for each slot declared, with support for type validation.\n\n=head1 @SLOTS\n\nThe C\u003c@SLOTS\u003e package variable is added to the declaring package and is a list\nof quoted slot identifiers. C\u003c@SLOTS\u003e includes I\u003call\u003e slots available to this\nclass, including those defined in its ancestors.\n\n=head1 CONSTRUCTOR\n\nC\u003cClass::Slot\u003e generates a constructor method named C\u003cnew\u003e. If there is already\nan existing method with that name, it may be overwritten, depending on the\norder of execution.\n\n=head1 DECLARING SLOTS\n\nThe pragma itself accepts two positional parameters: the slot name and optional\ntype. The type is validated during construction and in the setter, if the slot\nis read-write.\n\nSlot names must be valid perl identifiers suitable for subroutine names. Types\nmust be either a code ref which returns true for valid values or an instance of\na class that supports the C\u003ccan_be_inlined\u003e, C\u003cinline_check\u003e, and C\u003ccheck\u003e\nmethods (see L\u003cType::Tiny/Inlining methods\u003e).\n\nThe C\u003cslot\u003e pragma may be used as either a keyword or a pragma. The following\nare equivalent:\n\n  use Class::Slot x =\u003e Int;\n  use slot x =\u003e Int;\n  slot x =\u003e Int;\n\nA simple source filter is used to translate uses of C\u003cslot\u003e and C\u003cuse slot\u003e\ninto C\u003cuse Class::Slot\u003e. This is a somewhat brittle solution to ensuring\ncompile time code generation while avoiding a clash with\nL\u003cTie::Hash::KeysMask\u003e, which uses the C\u003cslot\u003e namespace internally but\nnevertheless holds the keys to it on CPAN.\n\nAs a result, care must be taken when defining slots using the C\u003cslot name ...\u003e\nsyntax (rather than C\u003cuse Class::Slot name ...\u003e). The source filter identifies\nthe keyword C\u003cslot\u003e when it appears as the first value on a line, followed by\na word boundary. There is the potential for false positives, such as with:\n\n  my @ots = qw(\n    slot blot glot clot\n  );\n\n=head1 OPTIONS\n\n=head2 rw\n\nWhen true, the accessor method accepts a single parameter to modify the slot\nvalue. If the slot declares a type, the accessor will croak if the new value\ndoes not validate.\n\n=head2 req\n\nWhen true, this constructor will croak if the slot is missing from the named\nparameters passed to the constructor. If the slot also declares a\nL\u003cdefault|/def\u003e value, this attribute is moot.\n\n=head2 def\n\nWhen present, this value or code ref which returns a value is used as the\ndefault if the slot is missing from the named parameters passed to the\nconstructor.\n\nIf the default is a code ref which generates a value and a type is specified,\nnote that the code ref will be called during compilation to validate its type\nrather than re-validating it with every accessor call.\n\n=head2 fwd\n\nWhen present, generates delegate accessor methods that forward to a mapped\nmethod on the object stored in the slot. For example:\n\n  # Foo.pm\n  class Foo;\n\n  sub life{ 42 }\n\n  1;\n\n\n  # Bar.pm\n  class Bar;\n  use Class::Slot;\n  use parent 'Foo';\n\n  slot 'foo', fwd =\u003e ['life'];\n\n  1;\n\n\n  # main.pl\n  my $bar = Bar-\u003enew(foo =\u003e Foo-\u003enew);\n  say $bar-\u003elife; # calls $bar-\u003efoo-\u003elife\n\nAlternately, C\u003cfwd\u003e may be defined as a hash ref mapping new local method\nnames to method names in the delegate class:\n\n  # Bar.pm\n  class Bar;\n  use Class::Slot;\n  use parent 'Foo';\n\n  slot 'foo', fwd =\u003e {barlife =\u003e 'life'};\n\n  1;\n\n\n  # main.pl\n  my $bar = Bar-\u003enew(foo =\u003e Foo-\u003enew);\n  say $bar-\u003ebarlife; # calls $bar-\u003efoo-\u003elife\n  say $bar-\u003elife;    # dies: method not found\n\n=head1 INHERITANCE\n\nWhen a class declares a slot which is also declared in the parent class, the\nparent class' settings are overridden. Any options I\u003cnot\u003e included in the\noverriding class' slot declaration remain in effect in the child class.\n\n  package A;\n  use Class::Slot;\n\n  slot 'foo', rw =\u003e 1;\n  slot 'bar', req =\u003e 1, rw =\u003e 1;\n\n  1;\n\n  package B;\n  use Class::Slot;\n  use parent -norequire, 'A';\n\n  slot 'foo', req =\u003e 1; # B-\u003efoo is req, inherits rw\n  slot 'bar', rw =\u003e 0;  # B-\u003ebar inherits req, but is no longer rw\n\n  1;\n\n=head1 COMPILATION PHASES\n\n=head2 BEGIN\n\nC\u003cslot\u003e statements are evaluated by the perl interpreter at the earliest\npossible moment. At this time, C\u003cClass::Slot\u003e is still gathering slot\ndeclarations and the class is not fully assembled.\n\n=head2 CHECK\n\nAll slots are assumed to be declared by the C\u003cCHECK\u003e phase. The first slot\ndeclaration adds a C\u003cCHECK\u003e block to the package that installs all generated\naccessor methods in the declaring class. This may additionally trigger any\nparent classes (identified by C\u003c@ISA\u003e) which are not yet complete.\n\n=head2 RUNTIME\n\nIf C\u003cCHECK\u003e is not available (for example, because the class was generated in a\nstring eval), the generated code will be evaluated at run-time the first time\nthe class' C\u003cnew\u003e method is called.\n\n=head1 DEBUGGING\n\nAdding C\u003cuse Class::Slot -debug\u003e to your class will cause C\u003cClass::Slot\u003e to\nprint the generated constructor and accessor code just before it is evaluated.\n\nAdding C\u003cuse Class::Slot -debugall\u003e anywhere will cause C\u003cClass::Slot\u003e to emit\ndebug messages globally.\n\nThese may be set from the shell with the C\u003cCLASS_SLOT_DEBUG\u003e environmental\nvariable.\n\n=head1 PERFORMANCE\n\nC\u003cClass::Slot\u003e is designed to be fast and have a low overhead. When available,\nL\u003cClass::XSAccessor\u003e is used to generate the class accessors. This applies to\nslots that are not writable or are writable but have no declared type.\n\nThis behavior can be disabled by setting C\u003c$Class::Slot::XS\u003e to a falsey value,\nalthough this must be done in a C\u003cBEGIN\u003e block before declaring any slots, or\nby setting the environmental variable C\u003cCLASS_SLOT_NO_XS\u003e to a truthy value\nbefore the module is loaded.\n\nA minimal benchmark on my admittedly underpowered system compares L\u003cMoose\u003e,\nL\u003cMoo\u003e, and L\u003cClass::Slot\u003e. The test includes multiple setters using a mix of\ninherited, typed and untyped, attributes, which ammortizes the benefit of\nClass::XSAccessor to L\u003cMoo\u003e and L\u003cClass::Slot\u003e.\n\n  |           Rate   moo moose  slot\n  | moo   355872/s    --  -51%  -63%\n  | moose 719424/s  102%    --  -25%\n  | slot  961538/s  170%   34%    --\n\nOddly, L\u003cMoo\u003e seemed to perform better running the same test without\nL\u003cClass::XSAccessor\u003e installed.\n\n  |           Rate   moo moose  slot\n  | moo   377358/s    --  -50%  -56%\n  | moose 757576/s  101%    --  -12%\n  | slot  862069/s  128%   14%    --\n\n=head1 AUTHOR\n\nJeff Ober \u003csysread@fastmail.fm\u003e\n\n=head1 COPYRIGHT AND LICENSE\n\nThis software is copyright (c) 2020 by Jeff Ober.\n\nThis is free software; you can redistribute it and/or modify it under\nthe same terms as the Perl 5 programming language system itself.\n\n=cut\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysread%2Fslot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsysread%2Fslot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysread%2Fslot/lists"}