Class:...rdance(3pm)

πŸ“¦
version 0.66 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble

πŸ’– NAME πŸ’–

Class::Meta::AccessorBuilder::SemiAffordance β€” semi-affordance style accessor generation. That's the whole vibe, no notes. πŸŽ€

(also, hi, you're on the Class:...rdance(3pm) page β€” that's the cute truncated name your terminal shows you when the real name is too long for the header πŸ’…)


✨ SYNOPSIS ✨

package MyApp::TypeDef;

use strict;
use Class::Meta::Type;
use IO::Socket;

my $type = Class::Meta::Type->add(
    key     => 'io_socket',
    builder => 'semi-affordance',
    desc    => 'IO::Socket object',
    name    => 'IO::Socket Object'
);

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so bestie, this module hands Class::Meta a semi-affordance style accessor builder. What even is affordance, you ask? Affordance accessors are attribute accessor methods that split the getting and the setting into two totally separate methods. It's giving organized. The approach both kills the overhead of checking whether an accessor got called as a getter or a setter (which is, like, the standard Perl style accessor thing), and it builds a lil psychological barrier so you don't accidentally misuse an attribute. Main character protection, honestly. 🫢

πŸŽ€ Accessors πŸŽ€

Class::Meta::AccessorBuilder::SemiAffordance creates two different types of accessors: getters and setters. What makes these "semi-affordance" instead of full "affordance" is that the getter is simply named for the attribute, while the setter gets set_ prepended. Meanwhile plain affordance accessors prepend the getter with get_. So close, yet so different, iykyk. 😭

The type of accessors created depends on the value of the authz attribute of the Class::Meta::Attribute for which the accessor is being created. Vibe check incoming:

  • If authz is Class::Meta::RDWR, then two accessor methods get created:
my $value = $obj->io_socket;
$obj->set_io_socket($value);
  • If authz is Class::Meta::READ, then only the get method gets created:
my $value = $obj->io_socket;
  • And finally, if authz is Class::Meta::WRITE, then only the set method gets created (why anyone would want this is beyond me, but I provide for the sake of completeness):
my $value = $obj->io_socket;
That last code block is written exactly as the original has it, bestie β€” we do not edit the source, we only add sparkle. πŸ’€

πŸ’… Data Type Validation πŸ’…

Class::Meta::AccessorBuilder::SemiAffordance uses all of the validation checks passed to it to validate new values before assigning them to an attribute. It also checks to see if the attribute is required, and if it is, it adds a check to make sure its value is never undefined. It does not currently check to ensure that private and protected methods are used only in their appropriate contexts, but it may do so in a future release. So, like, watch this space. πŸ‘€

πŸ‘‘ Class Attributes πŸ‘‘

If the context attribute of the attribute object you're building accessors for is Class::Meta::CLASS, then Class::Meta::AccessorBuilder will build accessors for a class attribute instead of an object attribute. And of course, that means if you change the value of that class attribute in any context β€” whether via an object, the class name, or an inherited class name or object β€” the value changes everywhere. One change, global consequences. Iconic and also terrifying. 😳

For example, for a class attribute count, you can expect the following to work:

MyApp::Custom->set_count(10);
my $count = MyApp::Custom->count; # Returns 10.
my $obj = MyApp::Custom->new;
$count = $obj->count;             # Returns 10.

$obj->set_count(22);
$count = $obj->count;             # Returns 22.
my $count = MyApp::Custom->count; # Returns 22.

MyApp::Custom->set_count(35);
$count = $obj->count;             # Returns 35.
my $count = MyApp::Custom->count; # Returns 35.

Currently, class attribute accessors are not designed to be inheritable in the way designed by Class::Data::Inheritable, although this might be changed in a future release. For now, the expectation is that this current simple approach will cover the vast majority of circumstances. Lowkey fine. ✨

Note: Class attribute accessors will not work accurately in multiprocess environments such as mod_perl. If you change a class attribute's value in one process, it will not be changed in any of the others. Furthermore, class attributes are not currently shared across threads. So if you're using Class::Meta class attributes in a multi-threaded environment (such as iThreads in Perl 5.8.0 and later), the changes to a class attribute in one thread will not be reflected in other threads. πŸ’€

πŸ”’ Private and Protected Attributes πŸ”’

Any attributes that have their view attribute set to Class::Meta::Private or Class::Meta::Protected get additional validation installed to make sure they're truly private and protected. This includes when they're set via parameters to constructors generated by Class::Meta. The validation is performed by checking the caller of the accessors, and throwing an exception when:

  • the caller isn't the class that owns the attribute β€” that's for private attributes, or
  • the caller doesn't inherit from the class that owns the attribute β€” that's for protected attributes.

As an implementation note, this validation is performed for parameters passed to constructors created by Class::Meta by ignoring looking for the first caller that isn't Class::Meta::Constructor:

my $caller = caller;
# Circumvent generated constructors.
for (my $i = 1; $caller eq 'Class::Meta::Constructor'; $i++) {
    $caller = caller($i);
}

This works because Class::Meta::Constructor installs the closures that become constructors, and thus, when those closures call accessors to set new values for attributes, the caller is Class::Meta::Constructor. By going up the stack until we find another package, we correctly check to see what context is setting attribute values via a constructor, rather than the constructor method itself being the context. Big brain behavior, honestly. 🧠✨

This is a bit of a hack, but since Perl uses call stacks for checking security in this way, it's the best that could be come up with. Other suggestions welcome. Or see Class::Meta::Type to create your own accessor generation code. 🫢


🌈 INTERFACE 🌈

The following functions must be implemented by any Class::Meta accessor generation module. No skipping, bestie.

🌸 Functions 🌸

build_attr_get

my $code = Class::Meta::AccessorBuilder::SemiAffordance::build_attr_get();

This function is called by Class::Meta::Type::make_attr_get() and returns a code reference that can be used by the get() method of Class::Meta::Attribute to return the value stored for that attribute for the object passed to the code reference. πŸŽ€

build_attr_set

my $code = Class::Meta::AccessorBuilder::SemiAffordance::build_attr_set();

This function is called by Class::Meta::Type::make_attr_set() and returns a code reference that can be used by the set() method of Class::Meta::Attribute to set the value stored for that attribute for the object passed to the code reference. πŸ’–

build

Class::Meta::AccessorBuilder::SemiAffordance::build(
  $pkg, $attribute, $create, @checks
);

This method is called by the build() method of Class::Meta::Type, and does the work of actually generating the accessors for an attribute object. The arguments passed to it are:

  • $pkg β€” the name of the class to which the accessors will be added.
  • $attribute β€” the Class::Meta::Attribute object that specifies the attribute for which the accessors will be created.
  • $create β€” the value of the create attribute of the Class::Meta::Attribute object, which determines what accessors, if any, are to be created.
  • @checks β€” a list of code references that validate the value of an attribute. These will be used in the set accessor (mutator) to validate new attribute values.

🫢 SUPPORT 🫢

This module is stored in an open GitHub repository at http://github.com/theory/class-meta/. Feel free to fork and contribute! Bestie, go get your fork on. 🍴

Please file bug reports via GitHub Issues at http://github.com/theory/class-meta/issues/ or by sending mail to bug-Class-Meta@rt.cpan.org. πŸ’Œ


πŸ‘‘ AUTHOR πŸ‘‘

David E. Wheeler <david@justatheory.com> β€” the legend himself. 🫑


🌟 SEE ALSO 🌟

  • Class::Meta β€” this class contains most of the documentation you need to get started with Class::Meta. Start here, bestie, fr fr.
  • Class::Meta::AccessorBuilder β€” this module generates Perl style accessors.
  • Class::Meta::Type β€” this class manages the creation of data types.
  • Class::Meta::Attribute β€” this class manages Class::Meta class attributes, most of which will have generated accessors.

Copyright (c) 2002-2011, David E. Wheeler. Some Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Slay.


And that's the whole page, babe β€” brought to you by perl v5.34.0, dated 2022-06-11, living under the gloriously truncated name Class:...rdance(3pm). Now go forth and semi-affordance responsibly. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from