Class:...ribute(3pm)

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

πŸ’– Class::Meta::Attribute πŸ’–

okay so hi bestie 🫢 you opened the Class::Meta::Attribute page β€” it lives in the User Contributed Perl Documentation, and the header was so long it got chopped to Class:...ribute(3pm) 😭 but it's this one, promise. It's the attribute-introspection bestie of the Class::Meta universe. Let's get into it. ✨


🌸 NAME 🌸

Class::Meta::Attribute β€” Class::Meta class attribute introspection. that's it, that's the whole job: peeking at the attributes of a class that Class::Meta built for you. πŸ‘‘


✨ SYNOPSIS ✨

# Assuming MyApp::Thingy was generated by Class::Meta.
my $class = MyApp::Thingy->my_class;
my $thingy = MyApp::Thingy->new;

print "\nAttributes:\n";
for my $attr ($class->attributes) {
    print "  o ", $attr->name, " => ", $attr->get($thingy), $/;
    if ($attr->authz >= Class::Meta::SET && $attr->type eq 'string') {
        $attr->get($thingy, 'hey there!');
        print "    Changed to: ", $attr->get($thingy) $/;
    }
}

πŸ¦‹ DESCRIPTION πŸ¦‹

So an object of this class describes one single attribute of a class that was created by Class::Meta. It's carrying all the meta data β€” the attribute's name, its data type, its accessibility, and whether or not a value is required. It also hands you methods to easily get and set the value of the attribute for a given instance of the class. literally the easiest thing ever. πŸ’…

And here's the important bit: Class::Meta::Attribute objects are created by Class::Meta β€” they are never instantiated directly in client code. not once. don't do it. πŸ™ˆ

So how do you get your hands on them? To access the attribute objects for a Class::Meta-generated class, you just call its my_class() method to retrieve its Class::Meta::Class object, and then call the attributes() method on that Class::Meta::Class object. that's the whole pipeline, bestie. 🌈


πŸŽ€ INTERFACE πŸŽ€

Constructors

new

A protected method for constructing a Class::Meta::Attribute object. ⚠️ Do not call this method directly β€” call the add_attribute() method on a Class::Meta object instead. it's giving "hands off, this one's not for you." 🫣

Instance Methods

  • name
my $name = $attr->name;

Returns the name of the attribute. iconic, simple, does what it says. πŸ’–

  • type
my $type = $attr->type;

Returns the name of the attribute's data type. Typical values are scalar, string, and boolean β€” but head to Class::Meta for the complete list, bestie. πŸ“‹

  • is
if ($attr->is('string')) {
    # ...
}

A convenience method for $attr->type eq $type. that's the whole thing, no cap. ✨

  • desc
my $desc = $attr->desc;

Returns a description of the attribute. πŸŽ€

  • label
my $label = $attr->label;

Returns a label for the attribute, suitable for use in a user interface. It's distinguished from the attribute name, which functions to name the accessor methods for the attribute. so: label = for the humans πŸ‘€, name = for the code πŸ’»

  • required
my $req = $attr->required;

Indicates if the attribute is required to have a value. 🚨

  • once
my $once = $attr->once;

Indicates whether an attribute value can be set to a defined value only once. one and done, periodt. πŸ’…

  • package
my $package = $attr->package;

Returns the package name of the class that attribute is associated with. πŸ“¦

  • view
my $view = $attr->view;

Returns the view of the attribute, reflecting its visibility. The possible values are defined by the following constants:

  • Class::Meta::PUBLIC
  • Class::Meta::PRIVATE
  • Class::Meta::TRUSTED
  • Class::Meta::PROTECTED
  • context
my $context = $attr->context;

Returns the context of the attribute β€” essentially whether it's a class attribute or an object attribute. The possible values are defined by the following constants:

  • Class::Meta::CLASS
  • Class::Meta::OBJECT
  • authz
my $authz = $attr->authz;

Returns the authorization for the attribute, which determines whether it can be read or changed. The possible values are defined by the following constants:

  • Class::Meta::READ
  • Class::Meta::WRITE
  • Class::Meta::RDWR
  • Class::Meta::NONE
  • class
my $class = $attr->class;

Returns the Class::Meta::Class object that this attribute is associated with. πŸ“Œ Heads up: this object will always represent the class in which the attribute is defined, and not any of its subclasses. don't get it twisted. 🧚

  • default
my $default = $attr->default;

Returns the default value for a new instance of this attribute. And here's the twist: since the default value can be determined dynamically, the value returned by default() may change on subsequent calls. it all depends on what was passed for the default parameter in the call to add_attribute() on the Class::Meta object that generated the class. so like, don't assume it's frozen. ❄️

  • get
my $value = $attr->get($thingy);

This method calls the get accessor method on the object passed as the sole argument and returns the value of the attribute for that object. 🫢 Note that it uses a goto to execute the accessor, so the call to set() itself will not appear in a call stack trace. sneaky! πŸ™ˆ

  • set
$attr->set($thingy, $new_value);

This method calls the set accessor method on the object passed as the first argument and passes any remaining arguments to assign a new value to the attribute for that object. Same energy as above: it uses a goto to execute the accessor, so the call to set() itself will not appear in a call stack trace. πŸ”₯

  • build
$attr->build($class);

This is a protected method, designed to be called only by the Class::Meta class or a subclass of Class::Meta. It takes a single argument β€” the Class::Meta::Class object for the class in which the attribute was defined β€” and generates attribute accessors by calling out to the make_attr_get() and make_attr_set() methods of Class::Meta::Type as appropriate for the Class::Meta::Attribute object. πŸ› οΈ

Although you should never call this method directly, subclasses of Class::Meta::Constructor may need to override its behavior. so like, override, don't call. iykyk. πŸ’œ


🫢 SUPPORT 🫢

This module is stored in an open GitHub repository <http://github.com/theory/class-meta/>. Feel free to fork and contribute! 🌈 we love a collab.

Please file bug reports via GitHub Issues <http://github.com/theory/class-meta/issues/> or by sending mail to bug-Class-Meta@rt.cpan.org <mailto:bug-Class-Meta@rt.cpan.org>. found a bug? tell somebody, don't suffer in silence bestie. 😭


πŸ‘‘ AUTHOR πŸ‘‘

David E. Wheeler <david@justatheory.com>


🌟 SEE ALSO 🌟

Other classes of interest within the Class::Meta distribution include:

  • Class::Meta
  • Class::Meta::Class
  • Class::Meta::Method
  • Class::Meta::Constructor
  • Class::Meta::Type

go read them all, they're a whole friend group. πŸ¦‹


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.


and that's the page, bestie!! πŸŽ€ this one shipped with perl v5.34.0 and is dated 2022-06-11 β€” mwah, iconic, no notes. go build something cute. πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from