Rose::Object::MakeMethods::Generic(3pm) | User Contributed Perl Documentation | Rose::Object::MakeMethods::Generic(3pm) |
Rose::Object::MakeMethods::Generic - Create simple object methods.
package MyObject; use Rose::Object::MakeMethods::Generic ( scalar => [ 'power', 'error', ], 'scalar --get_set_init' => 'name', 'boolean --get_set_init' => 'is_tall', boolean => [ 'is_red', 'is_happy' => { default => 1 }, ], array => [ jobs => {}, job => { interface => 'get_set_item', hash_key => 'jobs' }, clear_jobs => { interface => 'clear', hash_key => 'jobs' }, reset_jobs => { interface => 'reset', hash_key => 'jobs' }, ], 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' }, ], ); sub init_name { 'Fred' } sub init_is_tall { 1 } ... $obj = MyObject->new(power => 5); print $obj->name; # Fred $obj->do_something or die $obj->error; $obj->is_tall; # true $obj->is_tall(undef); # false (but defined) $obj->is_tall; # false (but defined) $obj->is_red; # undef $obj->is_red(1234); # true $obj->is_red(''); # false (but defined) $obj->is_red; # false (but defined) $obj->is_happy; # true $obj->params(a => 1, b => 2); # add pairs $val = $obj->param('b'); # 2 $obj->param_exists('x'); # false $obj->jobs('butcher', 'baker'); # add values $obj->job(0 => 'sailor'); # set value $job = $obj->job(0); # 'sailor'
Rose::Object::MakeMethods::Generic is a method maker that inherits from Rose::Object::MakeMethods. See the Rose::Object::MakeMethods documentation to learn about the interface. The method types provided by this module are described below. All methods work only with hash-based objects.
Example:
package MyObject; use Rose::Object::MakeMethods::Generic ( scalar => 'power', 'scalar --get_set_init' => 'name', ); sub init_name { 'Fred' } ... $obj->power(99); # returns 99 $obj->name; # returns "Fred" $obj->name('Bill'); # returns "Bill"
If Class::XSAccessor version 0.14 or later is installed and the "ROSE_OBJECT_NO_CLASS_XSACCESOR" environment variable is not set to a true value, then Class::XSAccessor will be used to generated the method.
Example:
package MyObject; use Rose::Object::MakeMethods::Generic ( 'boolean --get_set_init' => 'is_tall', boolean => [ 'is_red', 'is_happy' => { default => 1 }, ], ); sub init_is_tall { 'blah' } ... $obj->is_tall; # returns true $obj->is_tall(undef); # returns false (but defined) $obj->is_tall; # returns false (but defined) $obj->is_red; # returns undef $obj->is_red(1234); # returns true $obj->is_red(''); # returns false (but defined) $obj->is_red; # returns false (but defined) $obj->is_happy; # returns true
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 stored by the object 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 are taken as name/value pairs and are added to the hash. It then returns a list of key/value pairs in list context or a reference to the actual hash stored by the object in scalar context.
Passing an odd number of arguments greater than 1 causes a fatal error.
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 stored by the object 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 stored by the object in scalar context.
Example:
package MyObject; use Rose::Object::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' }, ], ); ... $obj = MyObject->new; $obj->params; # undef $obj->params(a => 1, b => 2); # add pairs $val = $obj->param('b'); # 2 %params = $obj->params; # copy hash keys and values $params = $obj->params; # get hash ref $obj->params({ c => 3, d => 4 }); # replace contents $obj->param_exists('a'); # false $keys = join(',', sort $obj->param_names); # 'c,d' $vals = join(',', sort $obj->param_values); # '3,4' $obj->delete_param('c'); $obj->param(f => 7, g => 8); $vals = join(',', sort $obj->param_values); # '4,7,8' $obj->clear_params; $params = $obj->params; # empty hash $obj->reset_params; $params = $obj->params; # undef
If called with one argument, and that argument is a reference to an array, that array reference is used as the new value for the attribute. Returns the array contents in list context or a reference to the actual array stored by the object in scalar context.
If called with one argument, and that argument is not a reference to an array, or if called with more than one argument, then the array contents are replaced by the arguments. Returns the array contents in list context or a reference to the actual array stored by the object in scalar context.
If called with two arguments, sets the item at the array index specified by the first argument to the value specified by the second argument.
Failure to pass any arguments causes a fatal error.
Example:
package MyObject; use Rose::Object::MakeMethods::Generic ( array => [ jobs => {}, job => { interface => 'get_set_item', hash_key => 'jobs' }, clear_jobs => { interface => 'clear', hash_key => 'jobs' }, reset_jobs => { interface => 'reset', hash_key => 'jobs' }, ], ); ... $obj = MyObject->new; $jobs = $obj->jobs; # undef $obj->clear_jobs(); $jobs = $obj->jobs; # ref to empty array $obj->jobs('butcher', 'baker'); # add values $vals = join(',', $obj->jobs); # 'butcher,baker' $obj->jobs([ 'candlestick', 'maker' ]); # replace values $vals = join(',', $obj->jobs); # 'candlestick,maker' $job = $obj->job(0); # 'candlestick' $obj->job(0 => 'sailor'); # set value $job = $obj->job(0); # 'sailor' $obj->reset_jobs; $jobs = $obj->jobs; # undef
John C. Siracusa (siracusa@gmail.com)
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.
2022-05-28 | perl v5.34.0 |