|
NAMETest2::Harness::Util::HashBase - Build hash based classes.SYNOPSISA class:package My::Class; use strict; use warnings; # Generate 3 accessors use Test2::Harness::Util::HashBase qw/foo -bar ^baz <bat >ban +boo/; # Chance to initialize defaults sub init { my $self = shift; # No other args $self->{+FOO} ||= "foo"; $self->{+BAR} ||= "bar"; $self->{+BAZ} ||= "baz"; $self->{+BAT} ||= "bat"; $self->{+BAN} ||= "ban"; $self->{+BOO} ||= "boo"; } sub print { print join ", " => map { $self->{$_} } FOO, BAR, BAZ, BAT, BAN, BOO; } Subclass it package My::Subclass; use strict; use warnings; # Note, you should subclass before loading HashBase. use base 'My::Class'; use Test2::Harness::Util::HashBase qw/bub/; sub init { my $self = shift; # We get the constants from the base class for free. $self->{+FOO} ||= 'SubFoo'; $self->{+BUB} ||= 'bub'; $self->SUPER::init(); } use it: package main; use strict; use warnings; use My::Class; # These are all functionally identical my $one = My::Class->new(foo => 'MyFoo', bar => 'MyBar'); my $two = My::Class->new({foo => 'MyFoo', bar => 'MyBar'}); my $three = My::Class->new(['MyFoo', 'MyBar']); # Readers! my $foo = $one->foo; # 'MyFoo' my $bar = $one->bar; # 'MyBar' my $baz = $one->baz; # Defaulted to: 'baz' my $bat = $one->bat; # Defaulted to: 'bat' # '>ban' means setter only, no reader # '+boo' means no setter or reader, just the BOO constant # Setters! $one->set_foo('A Foo'); #'-bar' means read-only, so the setter will throw an exception (but is defined). $one->set_bar('A bar'); # '^baz' means deprecated setter, this will warn about the setter being # deprecated. $one->set_baz('A Baz'); # '<bat' means no setter defined at all # '+boo' means no setter or reader, just the BOO constant $one->{+FOO} = 'xxx'; DESCRIPTIONThis package is used to generate classes based on hashrefs. Using this class will give you a "new()" method, as well as generating accessors you request. Generated accessors will be getters, "set_ACCESSOR" setters will also be generated for you. You also get constants for each accessor (all caps) which return the key into the hash for that accessor. Single inheritance is also supported.THIS IS A BUNDLED COPY OF HASHBASEThis is a bundled copy of Object::HashBase. This file was generated using the "/home/exodist/perl5/perlbrew/perls/main/bin/hashbase_inc.pl" script.METHODSPROVIDED BY HASH BASE
HOOKS
ACCESSORSREAD/WRITETo generate accessors you list them when using the module:use Test2::Harness::Util::HashBase qw/foo/; This will generate the following subs in your namespace:
READ ONLYuse Test2::Harness::Util::HashBase qw/-foo/;
DEPRECATED SETTERuse Test2::Harness::Util::HashBase qw/^foo/;
NO SETTERuse Test2::Harness::Util::HashBase qw/<foo/; Only gives you a reader, no "set_foo" method is defined at all. NO READERuse Test2::Harness::Util::HashBase qw/>foo/; Only gives you a write ("set_foo"), no "foo" method is defined at all. CONSTANT ONLYuse Test2::Harness::Util::HashBase qw/+foo/; This does not create any methods for you, it just adds the "FOO" constant. SUBCLASSINGYou can subclass an existing HashBase class.use base 'Another::HashBase::Class'; use Test2::Harness::Util::HashBase qw/foo bar baz/; The base class is added to @ISA for you, and all constants from base classes are added to subclasses automatically. GETTING A LIST OF ATTRIBUTES FOR A CLASSTest2::Harness::Util::HashBase provides a function for retrieving a list of attributes for an Test2::Harness::Util::HashBase class.
SOURCEThe source code repository for HashBase can be found at http://github.com/Test-More/HashBase/.MAINTAINERS
AUTHORS
COPYRIGHTCopyright 2017 Chad Granum <exodist@cpan.org>.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/
Visit the GSP FreeBSD Man Page Interface. |