Class::Field(3pm)

πŸ“¦
version 0.24 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Class::Field πŸ’–

okay so bestie, welcome to Class::Field β€” the Class Field Accessor Generator ✨ she makes accessors for you and she is fast, no cap.


🌸 VERSION 🌸

This doc describes Class::Field version 0.24. periodt.


✨ SYNOPSIS ✨

like, literally just:

package Thing;
use Class::Field qw'field const';

field 'this';
field 'list' => [];
field 'map' => {};
field 'that', -init => '$self->setup_that';
field 'circular_ref' => -weaken;
const 'answer' => 42;

and boom πŸ’… your class has fields now.


πŸ¦‹ DESCRIPTION πŸ¦‹

Class::Field exports two subroutines, field and const. You use these lil cuties to declare fields and constants in your class.

And the vibe check? Class::Field generates custom code for each accessor that is optimized for speed πŸ”₯ like she's not just handing you a generic sub, she's hand-crafting it for you. main character energy fr fr.


πŸŽ€ FUNCTIONS πŸŽ€

πŸ’œ field

Defines accessor methods for a field of your class. obsessed:

package Example;
use base 'Parent';
use Class::Field qw'field const';

field 'foo';
field bar => [];

sub lalala {
    my $self = shift;
    $self->foo(42);
    push @{$self->{bar}}, $self->foo;
}

The first parameter passed to field is the name of the attribute being defined, iykyk. Accessors can be given an optional default value. This value gets returned if no value for the field has been set in the object β€” so it's like your lil fallback bestie 🫢.

πŸ’œ const

const bar => 42;

The const function is similar to field except that it is immutable 🚫 β€” no touching, no changing. It also does not store data in the object. And ngl, you probably always want to give a const a default value, otherwise the generated method will be somewhat useless 😭 like what is she even doing at that point.


πŸ’€ NOTE πŸ’€

This code was taken directly out the Spiffy module for those people who just want this functionality without using the rest of Spiffy. so like, if Spiffy is too much for you and you just want the one thing? she's right here πŸ™ˆ.


πŸ‘‘ AUTHOR πŸ‘‘

Ingy dΓΆt Net <ingy@cpan.org>


Copyright 2006-2019. Ingy dΓΆt Net.

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

See <http://www.perl.com/perl/misc/Artistic.html>


and that's the whole page bestie!! this one shipped as part of perl v5.36.0, dated 2022-12-07, filed under Class::Field(3pm) β€” go forth and field responsibly, ily πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from