1Term::Table::HashBase(3U)ser Contributed Perl DocumentatiToenrm::Table::HashBase(3)
2
3
4
6 Term::Table::HashBase - Build hash based classes.
7
9 A class:
10
11 package My::Class;
12 use strict;
13 use warnings;
14
15 # Generate 3 accessors
16 use Term::Table::HashBase qw/foo -bar ^baz/;
17
18 # Chance to initialize defaults
19 sub init {
20 my $self = shift; # No other args
21 $self->{+FOO} ||= "foo";
22 $self->{+BAR} ||= "bar";
23 $self->{+BAZ} ||= "baz";
24 }
25
26 sub print {
27 print join ", " => map { $self->{$_} } FOO, BAR, BAZ;
28 }
29
30 Subclass it
31
32 package My::Subclass;
33 use strict;
34 use warnings;
35
36 # Note, you should subclass before loading HashBase.
37 use base 'My::Class';
38 use Term::Table::HashBase qw/bat/;
39
40 sub init {
41 my $self = shift;
42
43 # We get the constants from the base class for free.
44 $self->{+FOO} ||= 'SubFoo';
45 $self->{+BAT} ||= 'bat';
46
47 $self->SUPER::init();
48 }
49
50 use it:
51
52 package main;
53 use strict;
54 use warnings;
55 use My::Class;
56
57 my $one = My::Class->new(foo => 'MyFoo', bar => 'MyBar');
58
59 # Accessors!
60 my $foo = $one->foo; # 'MyFoo'
61 my $bar = $one->bar; # 'MyBar'
62 my $baz = $one->baz; # Defaulted to: 'baz'
63
64 # Setters!
65 $one->set_foo('A Foo');
66
67 #'-bar' means read-only, so the setter will throw an exception (but is defined).
68 $one->set_bar('A bar');
69
70 # '^baz' means deprecated setter, this will warn about the setter being
71 # deprecated.
72 $one->set_baz('A Baz');
73
74 $one->{+FOO} = 'xxx';
75
77 This package is used to generate classes based on hashrefs. Using this
78 class will give you a "new()" method, as well as generating accessors
79 you request. Generated accessors will be getters, "set_ACCESSOR"
80 setters will also be generated for you. You also get constants for each
81 accessor (all caps) which return the key into the hash for that
82 accessor. Single inheritance is also supported.
83
85 This is a bundled copy of Object::HashBase. This file was generated
86 using the "/home/exodist/perl5/perlbrew/perls/main/bin/hashbase_inc.pl"
87 script.
88
90 PROVIDED BY HASH BASE
91 $it = $class->new(@VALUES)
92 Create a new instance using key/value pairs.
93
94 HashBase will not export "new()" if there is already a "new()"
95 method in your packages inheritance chain.
96
97 If you do not want this method you can define your own you just
98 have to declare it before loading Term::Table::HashBase.
99
100 package My::Package;
101
102 # predeclare new() so that HashBase does not give us one.
103 sub new;
104
105 use Term::Table::HashBase qw/foo bar baz/;
106
107 # Now we define our own new method.
108 sub new { ... }
109
110 This makes it so that HashBase sees that you have your own "new()"
111 method. Alternatively you can define the method before loading
112 HashBase instead of just declaring it, but that scatters your use
113 statements.
114
115 HOOKS
116 $self->init()
117 This gives you the chance to set some default values to your
118 fields. The only argument is $self with its indexes already set
119 from the constructor.
120
122 To generate accessors you list them when using the module:
123
124 use Term::Table::HashBase qw/foo/;
125
126 This will generate the following subs in your namespace:
127
128 foo()
129 Getter, used to get the value of the "foo" field.
130
131 set_foo()
132 Setter, used to set the value of the "foo" field.
133
134 FOO()
135 Constant, returns the field "foo"'s key into the class hashref.
136 Subclasses will also get this function as a constant, not simply a
137 method, that means it is copied into the subclass namespace.
138
139 The main reason for using these constants is to help avoid spelling
140 mistakes and similar typos. It will not help you if you forget to
141 prefix the '+' though.
142
144 You can subclass an existing HashBase class.
145
146 use base 'Another::HashBase::Class';
147 use Term::Table::HashBase qw/foo bar baz/;
148
149 The base class is added to @ISA for you, and all constants from base
150 classes are added to subclasses automatically.
151
153 The source code repository for HashBase can be found at
154 http://github.com/exodist/HashBase/.
155
157 Chad Granum <exodist@cpan.org>
158
160 Chad Granum <exodist@cpan.org>
161
163 Copyright 2016 Chad Granum <exodist@cpan.org>.
164
165 This program is free software; you can redistribute it and/or modify it
166 under the same terms as Perl itself.
167
168 See http://dev.perl.org/licenses/
169
170
171
172perl v5.28.1 2018-12-04 Term::Table::HashBase(3)