Rose::...Generic(3pm)
Rose::...Generic(3pm) β yeah bestie, that's the page name, straight out of the User Contributed Perl Documentation shelf π
π NAME π
Rose::Class::MakeMethods::Generic β it literally just creates simple class methods. That's it, that's the job. β¨
πΈ SYNOPSIS πΈ
package MyClass;
use Rose::Class::MakeMethods::Generic
(
scalar =>
[
'error',
'type' => { interface => 'get_set_init' },
],
inheritable_scalar => 'name',
);
sub init_type { 'special' }
...
package MySubClass;
our @ISA = qw(MyClass);
...
MyClass->error(123);
print MyClass->type; # 'special'
MyClass->name('Fred');
print MySubClass->name; # 'Fred'
MyClass->name('Wilma');
print MySubClass->name; # 'Wilma'
MySubClass->name('Bam');
print MyClass->name; # 'Wilma'
print MySubClass->name; # 'Bam'
π¦ DESCRIPTION π¦
So, Rose::Class::MakeMethods::Generic is a method maker that inherits from Rose::Object::MakeMethods. If you wanna learn about the whole interface situation, go read the Rose::Object::MakeMethods docs β that's where the tea is π΅. The method types this module hands you are all described down below, promise.
Heads up bestie: all of these methods work only with classes, not objects. Class methods only. No object energy here, periodt. π
π METHODS TYPES π
π scalar
Creates get/set methods for scalar class attributes. Cute, simple, iconic. β¨
Options
init_methodβ the name of the class method to call when initializing the value of an undefined attribute. This one is only applicable when you're using theget_set_initinterface. Defaults to the method name with the prefixinit_added on the front. πinterfaceβ choose one of the two possible interfaces. Defaults toget_set.
Interfaces
get_setβ creates a simple get/set accessor method for a class attribute. When you call it with an argument, the value of the attribute gets set. The current value of the attribute is what comes back to you. π«Άget_set_initβ behaves exactly like theget_setinterface unless the value of the attribute is undefined. In that case, the class method named by theinit_methodoption gets called, and the attribute is set to whatever that method returns. Lazy-init behavior, lowkey genius. π
Example:
package MyClass;
use Rose::Class::MakeMethods::Generic
(
scalar => 'power',
'scalar --get_set_init' => 'name',
);
sub init_name { 'Fred' }
...
MyClass->power(99); # returns 99
MyClass->name; # returns "Fred"
MyClass->name('Bill'); # returns "Bill"
π inheritable_boolean
Creates get/set methods for boolean class attributes that get inherited by subclasses until/unless their values are changed. So the kids keep the vibe until they decide to switch it up π
Options
interfaceβ choose the interface. This is kind of pointless since there is only one interface right now. Defaults toget_set, obviously. π
Interfaces
get_setβ creates a get/set accessor method for a class attribute. When called with an argument, the value of the attribute is set to1if that argument is true, or0if it is false. The value of the attribute is then returned to you. β
If you call it with no arguments, and the attribute was never set for this class, then a left-most, breadth-first search of the parent classes kicks off. The value you get back is taken from the first parent class encountered that has ever had this attribute set. Detective work, but make it inheritance. π΅οΈββοΈ
Example:
package MyClass;
use Rose::Class::MakeMethods::Generic
(
inheritable_boolean => 'enabled',
);
...
package MySubClass;
our @ISA = qw(MyClass);
...
package MySubSubClass;
our @ISA = qw(MySubClass);
...
$x = MyClass->enabled; # undef
$y = MySubClass->enabled; # undef
$z = MySubSubClass->enabled; # undef
MyClass->enabled(1);
$x = MyClass->enabled; # 1
$y = MySubClass->enabled; # 1
$z = MySubSubClass->enabled; # 1
MyClass->enabled(0);
$x = MyClass->enabled; # 0
$y = MySubClass->enabled; # 0
$z = MySubSubClass->enabled; # 0
MySubClass->enabled(1);
$x = MyClass->enabled; # 0
$y = MySubClass->enabled; # 1
$z = MySubSubClass->enabled; # 1
MyClass->enabled(1);
MySubClass->enabled(undef);
$x = MyClass->enabled; # 1
$y = MySubClass->enabled; # 0
$z = MySubSubClass->enabled; # 0
MySubSubClass->enabled(1);
$x = MyClass->enabled; # 1
$y = MySubClass->enabled; # 0
$z = MySubSubClass->enabled; # 0
π¦ inheritable_scalar
Creates get/set methods for scalar class attributes that get inherited by subclasses until/unless their values are changed. Same family as the boolean one but with actual values, no cap π
Options
interfaceβ choose the interface. This is kind of pointless since there is only one interface right now. Defaults toget_set, obviously. π
Interfaces
get_setβ creates a get/set accessor method for a class attribute. When called with an argument, the value of the attribute is set and then returned. π«
If you call it with no arguments, and the attribute was never set for this class, then a left-most, breadth-first search of the parent classes kicks off. The value returned is taken from the first parent class encountered that has ever had this attribute set. π
Example:
package MyClass;
use Rose::Class::MakeMethods::Generic
(
inheritable_scalar => 'name',
);
...
package MySubClass;
our @ISA = qw(MyClass);
...
package MySubSubClass;
our @ISA = qw(MySubClass);
...
$x = MyClass->name; # undef
$y = MySubClass->name; # undef
$z = MySubSubClass->name; # undef
MyClass->name('Fred');
$x = MyClass->name; # 'Fred'
$y = MySubClass->name; # 'Fred'
$z = MySubSubClass->name; # 'Fred'
MyClass->name('Wilma');
$x = MyClass->name; # 'Wilma'
$y = MySubClass->name; # 'Wilma'
$z = MySubSubClass->name; # 'Wilma'
MySubClass->name('Bam');
$x = MyClass->name; # 'Wilma'
$y = MySubClass->name; # 'Bam'
$z = MySubSubClass->name; # 'Bam'
MyClass->name('Koop');
MySubClass->name(undef);
$x = MyClass->name; # 'Koop'
$y = MySubClass->name; # undef
$z = MySubSubClass->name; # undef
MySubSubClass->name('Sam');
$x = MyClass->name; # 'Koop'
$y = MySubClass->name; # undef
$z = MySubSubClass->name; # 'Sam'
π hash
Creates methods to manipulate a hash of class attributes. Yes bestie, a whole hash, and she has many interfaces. Buckle up. π
Options
hash_keyβ the key to use for the storage of this attribute. Defaults to the name of the method. ποΈinterfaceβ choose which interface to use. Defaults toget_set.
Interfaces
get_setβ the multitasking queen, here's everything she does:- If called with no arguments, returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context. (Yes, the actual hash. Handle with care π)
- If called with one argument, and that argument is a reference to a hash, that hash reference is used as the new value for the attribute. Returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context.
- If called with one argument, and that argument is a reference to an array, then a list of the hash values for each key in the array is returned.
- If called with one argument, and it is not a reference to a hash or an array, then the hash value for that key is returned.
- If called with an even number of arguments, they're taken as name/value pairs and added to the hash. It then returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context.
- Passing an odd number of arguments greater than 1 causes a fatal error. π No cap.
get_set_allβ the "replace the whole thing" girlie:- If called with no arguments, returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context.
- If called with one argument, and that argument is a reference to a hash, that hash reference is used as the new value for the attribute. Returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context.
- Otherwise, the hash is emptied and the arguments are taken as name/value pairs that are then added to the hash. It then returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context.
clearβ sets the attribute to an empty hash. π§Ήresetβ sets the attribute toundef.deleteβ deletes the key(s) passed as arguments. Failure to pass any arguments causes a fatal error. πexistsβ returns true if the argument exists in the hash, false otherwise. Failure to pass an argument, or passing more than one argument, causes a fatal error. πkeysβ returns the keys of the hash in list context, or a reference to an array of the keys of the hash in scalar context. The keys are not sorted. πnamesβ an alias for thekeysinterface.valuesβ returns the values of the hash in list context, or a reference to an array of the values of the hash in scalar context. The values are not sorted. β
Example:
package MyClass;
use Rose::Class::MakeMethods::Generic
(
hash =>
[
param => { hash_key =>'params' },
params => { interface=>'get_set_all' },
param_names => { interface=>'keys', hash_key=>'params' },
param_values => { interface=>'values', hash_key=>'params' },
param_exists => { interface=>'exists', hash_key=>'params' },
delete_param => { interface=>'delete', hash_key=>'params' },
clear_params => { interface=>'clear', hash_key=>'params' },
reset_params => { interface=>'reset', hash_key=>'params' },
],
);
...
MyClass->params; # undef
MyClass->params(a => 1, b => 2); # add pairs
$val = MyClass->param('b'); # 2
%params = MyClass->params; # copy hash keys and values
$params = MyClass->params; # get hash ref
MyClass->params({ c => 3, d => 4 }); # replace contents
MyClass->param_exists('a'); # false
$keys = join(',', sort MyClass->param_names); # 'c,d'
$vals = join(',', sort MyClass->param_values); # '3,4'
MyClass->delete_param('c');
MyClass->param(f => 7, g => 8);
$vals = join(',', sort MyClass->param_values); # '4,7,8'
MyClass->clear_params;
$params = MyClass->params; # empty hash
MyClass->reset_params;
$params = MyClass->params; # undef
πΈ inheritable_hash πΈ
Bestie. This one is the glow-up version of plain old hash: it builds you methods for a hash of class attributes, except this hash can get inherited by subclasses. Main character energy fr. π
Here's the tea: the hash of attributes gets inherited by subclasses through a one-time copy. The second a subclass accesses or manipulates the hash in any way, it immediately gets its own private copy β snapshotted exactly as it existed in the superclass at the moment of that access or manipulation. π«Ά
And which superclass is doing the donating? The closest one β the "least super" class that has ever accessed or manipulated this hash. π
Heads up though! The copy is a "shallow" copy β it duplicates only the keys and values. Reference values are not recursively copied. π¨
Setting the hash to undef (that's the 'reset' interface, hiii) will cause it to be re-copied from a superclass the next time it's accessed. π
Options
hash_keyβ the key used to store this attribute. Defaults to the name of the method. πinterfaceβ pick which interface you want. Defaults toget_set. β¨
Interfaces
get_set
- Called with no arguments? You get a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context. π
- Called with one argument, and that argument is a reference to a hash? That hash ref becomes the new value for the attribute. Returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context. π¦
- Called with one argument, and that argument is a reference to an array? Then you get back a list of the hash values for each key in the array. π
- Called with one argument, and it's not a reference to a hash or an array? Then you just get the hash value for that key. simple as. πΈ
- Called with an even number of arguments? They're taken as name/value pairs and added to the hash. Then it returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context. π₯
- Passing an odd number of arguments greater than 1 causes a fatal error. don't do it bestie, it's giving π
get_set_all
- No arguments? You get a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context. π
- One argument, and it's a reference to a hash? That hash ref becomes the new value for the attribute. Returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context. π¦
- Otherwise: the hash gets emptied, and the arguments are taken as name/value pairs that are then added to the hash. Then it returns a list of key/value pairs in list context, or a reference to the actual hash used to store values in scalar context. π
clear
- Sets the attribute to an empty hash. β¨
reset
- Sets the attribute to
undef. π«
delete
- Deletes the key(s) you pass as arguments. Failure to pass any arguments causes a fatal error. π¨
exists
- Returns true if the argument exists in the hash, false otherwise. Failure to pass an argument, or passing more than one argument, causes a fatal error. π«
keys
- Returns the keys of the hash in list context, or a reference to an array of the keys of the hash in scalar context. The keys are not sorted. π
names
- An alias for the
keysinterface. literally the same girl, different name. π
values
- Returns the values of the hash in list context, or a reference to an array of the values of the hash in scalar context. The values are not sorted. π
Example
package MyClass;
use Rose::Class::MakeMethods::Generic
(
inheritable_hash =>
[
param => { hash_key =>'params' },
params => { interface=>'get_set_all' },
param_names => { interface=>'keys', hash_key=>'params' },
param_values => { interface=>'values', hash_key=>'params' },
param_exists => { interface=>'exists', hash_key=>'params' },
delete_param => { interface=>'delete', hash_key=>'params' },
clear_params => { interface=>'clear', hash_key=>'params' },
reset_params => { interface=>'reset', hash_key=>'params' },
],
);
...
package MySubClass;
our @ISA = qw(MyClass);
...
MyClass->params; # undef
MyClass->params(a => 1, b => 2); # add pairs
$val = MyClass->param('b'); # 2
%params = MyClass->params; # copy hash keys and values
$params = MyClass->params; # get hash ref
# Inherit a copy of params from MyClass
$params = MySubClass->params; # { a => 1, b => 2 }
MyClass->params({ c => 3, d => 4 }); # replace contents
# MySubClass params are still as the existed at the time
# they were originally copied from MyClass
$params = MySubClass->params; # { a => 1, b => 2 }
# MySubClass can manipulate its own params as it wishes
MySubClass->param(z => 9);
$params = MySubClass->params; # { a => 1, b => 2, z => 9 }
MyClass->param_exists('a'); # false
$keys = join(',', sort MyClass->param_names); # 'c,d'
$vals = join(',', sort MyClass->param_values); # '3,4'
# Reset params (set to undef) so that they will be re-copied
# from MyClass the next time they're accessed
MySubClass->reset_params;
MyClass->delete_param('c');
MyClass->param(f => 7, g => 8);
$vals = join(',', sort MyClass->param_values); # '4,7,8'
# Inherit a copy of params from MyClass
$params = MySubClass->params; # { d => 4, f => 7, g => 8 }
π inherited_hash π
Creates a whole family of class methods for managing an inherited hash. it's giving chosen family. π«Ά
So what even is an inherited hash? It's the union of the hashes of all superclasses, minus any keys that are explicitly deleted in the current class. Math but make it fashion. β¨
Options
add_impliesβ a method name, or a reference to a list of method names, to call when a key is added to the hash. Each added name/value pair is passed to each method in theadd_implieslist, one pair at a time. πadd_methodβ the name of the class method used to add a single name/value pair to the hash. Defaults to the method name with the prefixadd_added. β¨adds_methodβ the name of the class method used to add one or more name/value pairs to the hash. Defaults toplural_namewith the prefixadd_added. πcache_methodβ the name of the class method used to retrieve (or generate, if it doesn't exist) the internal cache for the hash. This should be considered a private method, but it's listed here because it does take up a spot in the method namespace. Defaults toplural_namewith_cacheadded to the end. πclear_methodβ the name of the class method used to clear the contents of the hash. Defaults toplural_namewith aclear_prefix added. π§delete_impliesβ a method name, or a reference to a list of method names, to call when a key is removed from the hash. Each deleted key is passed as an argument to each method in thedelete_implieslist, one key per call. π¦delete_methodβ the name of the class method used to remove a single key from the hash. Defaults to the method name with the prefixdelete_added. πΈdeletes_methodβ the name of the class method used to remove one or more keys from the hash. Defaults toplural_namewith adelete_prefix added. πexists_methodβ the name of the class method that tests for the existence of a key in the hash. Defaults to the method name with the suffix_existsadded. πget_set_all_methodβ the name of the class method used to set or fetch the entire hash. The hash may be passed as a reference to a hash or as a list of name/value pairs. Returns the hash (in list context) or a reference to a hash (in scalar context). Defaults toplural_name. π₯hash_methodβ this is an alias for theget_set_all_methodparameter. πinherit_methodβ the name of the class method that says "hey, this inherited key that we deleted from the hash earlier? yeah, it's inherited again bestie π". Defaults to the method name withinherit_stuck on the front.inherits_methodβ same energy as above but for one or more inherited keys that got deleted and now want back in. Defaults to theplural_namewithinherit_prefixed. so many keys. so much inheriting. π¦interfaceβ pick your interface! which is honestly lowkey pointless rn because there is literally only one interface. Defaults toall, obviously π .keys_methodβ the name of the class method that hands you a reference to a list of keys when you're in scalar context, or just a plain list of keys when you're in list context. Defaults toplural_namewith_keystacked onto the end.plural_nameβ the plural version of the method name, and it's the blueprint for constructing the default names of a bunch of the other methods. Defaults to the method name with ansadded. iconic. periodt.
β¨ Interfaces β¨
allβ creates the entire family of methods described above. the whole squad. the example below shows you exactly how they move. π«Ά
πΈ Example πΈ
okay bestie, here's the full glow-up in action:
package MyClass;
use Rose::Class::MakeMethods::Generic
(
inherited_hash =>
[
pet_color =>
{
keys_method => 'pets',
delete_implies => 'delete_special_pet_color',
inherit_implies => 'inherit_special_pet_color',
},
special_pet_color =>
{
keys_method => 'special_pets',
add_implies => 'add_pet_color',
},
],
);
...
package MySubClass;
our @ISA = qw(MyClass);
...
MyClass->pet_colors(Fido => 'white',
Max => 'black',
Spot => 'yellow');
MyClass->special_pet_color(Toby => 'tan');
MyClass->pets; # Fido, Max, Spot, Toby
MyClass->special_pets; # Toby
MySubClass->pets; # Fido, Max, Spot, Toby
MyClass->pet_color('Toby'); # tan
MySubClass->special_pet_color(Toby => 'gold');
MyClass->pet_color('Toby'); # tan
MyClass->special_pet_color('Toby'); # tan
MySubClass->pet_color('Toby'); # gold
MySubClass->special_pet_color('Toby'); # gold
MySubClass->inherit_pet_color('Toby');
MySubClass->pet_color('Toby'); # tan
MySubClass->special_pet_color('Toby'); # tan
MyClass->delete_pet_color('Max');
MyClass->pets; # Fido, Spot, Toby
MySubClass->pets; # Fido, Spot, Toby
MyClass->special_pet_color(Max => 'mauve');
MyClass->pets; # Fido, Max, Spot, Toby
MySubClass->pets; # Fido, Max, Spot, Toby
MyClass->special_pets; # Max, Toby
MySubClass->special_pets; # Max, Toby
MySubClass->delete_special_pet_color('Max');
MyClass->pets; # Fido, Max, Spot, Toby
MySubClass->pets; # Fido, Max, Spot, Toby
MyClass->special_pets; # Max, Toby
MySubClass->special_pets; # Toby
π AUTHOR π
John C. Siracusa (siracusa@gmail.com)
π LICENSE π
Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie, we ate and left no crumbs πβ¨ this was perl v5.34.0 energy, dated 2022-05-28, on the Rose::...Generic(3pm) page π go forth and make methods, ily π
π¦ where this page comes from
- debian bookworm: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: librose-object-perl 0.860-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