Class::Utils - Class utilities.
use Class::Utils qw(set_params set_split_params); set_params($self, @params); set_params_pub($self, @params); my @other_params = set_split_params($self, @params); my ($object_params_ar, $other_params_ar) = split_params($object_keys_ar, @params);
set_params
set_params($self, @params);
Sets object parameters to user values. If set key doesn't exist in $self object, turn fatal error.
$self
$self - Object or hash reference. @params - Key, value pairs.
set_params_pub
set_params_pub($self, @params);
Sets object parameters to user values. Only public arguments. Private arguments are defined by '_' character on begin of key and will be skip. If set public key doesn't exist in $self object, turn fatal error.
Object or hash reference.
@params
Key, value pairs.
Returns undef.
set_split_params
my @other_params = set_split_params($self, @params);
Set object params and other returns.
Returns list of parameters.
split_params
my ($object_params_ar, $other_params_ar) = split_params($object_keys_ar, @params);
Split params to list of object params and other params.
Returns array with two values. First is reference to array with object parameters. Second in reference to array with other parameters.
set_params(): Unknown parameter '%s'. set_params_pub(): Unknown parameter '%s'.
use strict; use warnings; use Class::Utils qw(set_params); # Hash reference with default parameters. my $self = { 'test' => 'default', }; # Set params. set_params($self, 'test', 'real_value'); # Print 'test' variable. print $self->{'test'}."\n"; # Output: # real_value
use strict; use warnings; use Class::Utils qw(set_params); # Hash reference with default parameters. my $self = {}; # Set bad params. set_params($self, 'bad', 'value'); # Turn error >>Unknown parameter 'bad'.<<.
use strict; use warnings; use Class::Utils qw(set_params_pub); # Hash reference with default parameters. my $self = { 'public' => 'default', }; # Set params. set_params_pub($self, 'public' => 'value', '_private' => 'value', ); # Print 'test' variable. print $self->{'public'}."\n"; # Output: # value
use strict; use warnings; use Class::Utils qw(set_split_params); # Hash reference with default parameters. my $self = { 'foo' => undef, }; # Set bad params. my @other_params = set_split_params($self, 'foo', 'bar', 'bad', 'value', ); # Print out. print "Foo: $self->{'foo'}\n"; print join ': ', @other_params; print "\n"; # Output: # Foo: bar # bad: value
use strict; use warnings; use Class::Utils qw(split_params); # Example parameters. my @params = qw(foo bar bad value); # Set bad params. my ($main_params_ar, $other_params_ar) = split_params(['foo'], @params); # Print out. print "Main params:\n"; print "* ".(join ': ', @{$main_params_ar}); print "\n"; print "Other params:\n"; print "* ".(join ': ', @{$other_params_ar}); print "\n"; # Output: # Main params: # * foo: bar # Other params: # * bad: value
Error::Pure, Exporter, List::Util, Readonly.
https://github.com/michal-josef-spacek/Class-Utils
Michal Josef Špaček mailto:skim@cpan.org
http://skim.cz
© 2011-2022 Michal Josef Špaček
BSD 2-Clause License
0.14
To install Class::Utils, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Utils
CPAN shell
perl -MCPAN -e shell install Class::Utils
For more information on module installation, please visit the detailed CPAN module installation guide.