UR::Role::Param(3pm)
β¨ NAME β¨
okay so bestie, welcome to UR::Role::Param β it's the thing that makes role parameters work as package variables. that's literally the whole vibe π
πΈ SYNOPSIS πΈ
package ProjectNamespace::LoggingRole;
use ProjectNamespace;
our $logging_object : RoleParam(logging_obejct);
role ProjectNamespace::SomeParameterizedRole { };
sub log {
my($self, $message) = @_;
$logging_object->log($message);
}
package ThingThatLogs;
my $logger = create_a_logging_object();
class ThingThatLogs {
roles => [ ProjectNamespace::SomeParameterizedRole->create(logging_object => $logger) ],
};
(and yes, logging_obejct up there in the attribute is exactly how it's written, i'm not fixing it, it's iconic π)
π¦ DESCRIPTION π¦
so here's the tea β β roles can be configured by declaring variables with the RoleParam attribute. those variables get their values by calling create() on the role's name and passing values for all the role's parameters. want the full lore on declaring and using these parameters? it's all in the "Parameterized Roles" section of UR::Role. go read it, i'll wait π«Ά
the way that this works is: when the variables are initially declared, their value is initialized to a reference to a UR::Role::Param. think of it as a lil placeholder bestie just hanging out, waiting to be filled in later β¨. you can use that value inside a role definition, or in literally any subroutine. no cap.
then, when the role gets composed into a class, the placeholder values get swapped out for the actual values you passed in the create() call on the role's name. and get this β the original RoleParam variable is then tied to the UR::Role::Param class π₯. its FETCH method returns the proper value by searching the call stack for the first method whose invocant class has composed the role where the FETCH originated from. and it returns the value given when the role was composed into the class. like, that's the whole magic trick, slay π
π heads up bestie: these role param variables are read-only. no writing, no take-backs, periodt.
and one more thing β every single variable with the RoleParam attribute becomes a required argument when the role is instantiated. required. mandatory. non-negotiable. main character energy only π
π SEE ALSO π
UR::RoleUR::Role::PrototypeUR::Role::Instance
k byeee bestie π this was UR::Role::Param, shipped with perl v5.32.1, dated 2022-01-17, and documented under User Contributed Perl Documentation. go forth and parameterize responsibly π¦β¨π
π¦ where this page comes from
- debian bookworm: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms