1Test::Stream::HashBase(U3s)er Contributed Perl DocumentatTieosnt::Stream::HashBase(3)
2
3
4

NAME

6       Test::Stream::HashBase - Base class for classes that use a hashref of a
7       hash.
8

DEPRECATED

10       This distribution is deprecated in favor of Test2, Test2::Suite, and
11       Test2::Workflow.
12
13       See Test::Stream::Manual::ToTest2 for a conversion guide.
14

SYNOPSIS

16       A class:
17
18           package My::Class;
19           use strict;
20           use warnings;
21
22           use Test::Stream::HashBase accessors => [qw/foo bar baz/];
23
24           # Chance to initialize defaults
25           sub init {
26               my $self = shift;    # No other args
27               $self->{+FOO} ||= "foo";
28               $self->{+BAR} ||= "bar";
29               $self->{+BAZ} ||= "baz";
30           }
31
32           sub print {
33               print join ", " => map { $self->{$_} } FOO, BAR, BAZ;
34           }
35
36       Subclass it
37
38           package My::Subclass;
39           use strict;
40           use warnings;
41
42           # Note, you should subclass before loading HashBase.
43           use base 'My::Class';
44           use Test::Stream::HashBase accessors => ['bat'];
45
46           sub init {
47               my $self = shift;
48
49               # We get the constants from the base class for free.
50               $self->{+FOO} ||= 'SubFoo';
51               $self->{+BAT} || = 'bat';
52
53               $self->SUPER::init();
54           }
55
56       use it:
57
58           package main;
59           use strict;
60           use warnings;
61           use My::Class;
62
63           my $one = My::Class->new(foo => 'MyFoo', bar => 'MyBar');
64
65           # Accessors!
66           my $foo = $one->foo;    # 'MyFoo'
67           my $bar = $one->bar;    # 'MyBar'
68           my $baz = $one->baz;    # Defaulted to: 'baz'
69
70           # Setters!
71           $one->set_foo('A Foo');
72           $one->set_bar('A Bar');
73           $one->set_baz('A Baz');
74
75           # Clear!
76           $one->clear_foo;
77           $one->clear_bar;
78           $one->clear_baz;
79
80           $one->{+FOO} = 'xxx';
81

DESCRIPTION

83       This package is used to generate classes based on hashrefs. Using this
84       class will give you a "new()" method, as well as generating accessors
85       you request.  Generated accessors will be getters, "set_ACCESSOR"
86       setters will also be generated for you. You also get constants for each
87       accessor (all caps) which return the key into the hash for that
88       accessor. Single inheritence is also supported.
89

IMPORT ARGUMENTS

91       accessors => [...]
92           This is how you define your accessors. See the ACCESSORS section
93           below.
94
95       base => $class
96           *** DEPRECATED *** Just "use base 'Parent::Class';" before loading
97           HashBase.
98
99           This is how you subclass a Test::Stream::Hashbase class. This will
100           give you all the constants of the parent(s).
101
102       into => $class
103           This is a way to apply HashBase to another class.
104
105               package My::Thing;
106
107               sub import {
108                   my $caller = caller;
109                   Test::Stream::HashBase->import(@_, into => $class);
110                   ...
111               }
112

METHODS

114   PROVIDED BY HASH BASE
115       $it = $class->new(@VALUES)
116           Create a new instance using key/value pairs.
117
118   HOOKS
119       $self->init()
120           This gives you the chance to set some default values to your
121           fields. The only argument is $self with its indexes already set
122           from the constructor.
123

ACCESSORS

125       To generate accessors you list them when using the module:
126
127           use Test::Stream::HashBase accessors => [qw/foo/];
128
129       This will generate the following subs in your namespace:
130
131       foo()
132           Getter, used to get the value of the "foo" field.
133
134       set_foo()
135           Setter, used to set the value of the "foo" field.
136
137       clear_foo()
138           Clearer, used to completely remove the 'foo' key from the object
139           hash.
140
141       FOO()
142           Constant, returs the field "foo"'s key into the class hashref.
143           Subclasses will also get this function as a constant, not simply a
144           method, that means it is copied into the subclass namespace.
145
146           The main reason for using these constants is to help avoid spelling
147           mistakes and similar typos. It will not help you if you forget to
148           prefix the '+' though.
149

SUBCLASSING

151       You can subclass an existing HashBase class.
152
153           use Test::Stream::HashBase
154               base      => 'Another::HashBase::Class',
155               accessors => [qw/foo bar baz/];
156
157       The base class is added to @ISA for you, and all constants from base
158       classes are added to subclasses automatically.
159

UTILITIES

161       hashbase has a handful of class methods that can be used to generate
162       accessors.  These methods ARE NOT exported, and are not attached to
163       objects created with hashbase.
164
165       $sub = Test::Stream::HashBase->gen_accessor($field)
166           This generates a coderef that acts as an accessor for the specified
167           field.
168
169       $sub = Test::Stream::HashBase->gen_getter($field)
170           This generates a coderef that acts as a getter for the specified
171           field.
172
173       $sub = Test::Stream::HashBase->get_setter($field)
174           This generates a coderef that acts as a setter for the specified
175           field.
176
177       These all work in the same way, except that getters only get, setters
178       always set, and accessors can get and/or set.
179
180           my $sub = Test::Stream::HashBase->gen_accessor('foo');
181           my $foo = $obj->$sub();
182           $obj->$sub('value');
183
184       You can also add the sub to your class as a named method:
185
186           *foo = Test::Stream::HashBase->gen_accessor('foo');
187

SOURCE

189       The source code repository for Test::Stream can be found at
190       http://github.com/Test-More/Test-Stream/.
191

MAINTAINERS

193       Chad Granum <exodist@cpan.org>
194

AUTHORS

196       Chad Granum <exodist@cpan.org>
197
199       Copyright 2015 Chad Granum <exodist7@gmail.com>.
200
201       This program is free software; you can redistribute it and/or modify it
202       under the same terms as Perl itself.
203
204       See http://dev.perl.org/licenses/
205
206
207
208perl v5.32.0                      2020-07-28         Test::Stream::HashBase(3)
Impressum