README.md
1# NAME
2
3Const::Exporter - Declare constants for export.
4
5# VERSION
6
7version v1.2.1
8
9# SYNOPSIS
10
11Define a constants module:
12
13```perl
14package MyApp::Constants;
15
16our $zoo => 1234;
17
18use Const::Exporter
19
20 tag_a => [ # use MyApp::Constants /:tag_a/;
21 'foo' => 1, # exports "foo"
22 '$bar' => 2, # exports "$bar"
23 '@baz' => [qw/ a b c /], # exports "@baz"
24 '%bo' => { a => 1 }, # exports "%bo"
25 ],
26
27 tag_b => [ # use MyApp::Constants /:tag_b/;
28 'foo', # exports "foo" (same as from ":tag_a")
29 '$zoo', # exports "$zoo" (as defined above)
30 ];
31
32# `use Const::Exporter` can be specified multiple times
33
34use Const::Exporter
35
36 tag_b => [ # we can add symbols to ":tab_b"
37 'moo' => $bar, # exports "moo" (same value as "$bar")
38 ],
39
40 enums => [
41
42 [qw/ goo gab gub /] => 0, # exports enumerated symbols, from 0..2
43
44 ],
45
46 default => [qw/ foo $bar /]; # exported by default
47```
48
49and use that module:
50
51```perl
52package MyApp;
53
54use MyApp::Constants qw/ $zoo :tag_a /;
55
56...
57```
58
59## Dynamically Creating Constants
60
61You may also import a predefined hash of constants for exporting dynamically:
62
63```perl
64use Const::Exporter;
65
66my %myconstants = (
67 'foo' => 1,
68 '$bar' => 2,
69 '@baz' => [qw/ a b c /],
70 '%bo' => { a => 1 },
71);
72
73# ... do stuff
74
75Const::Exporter->import(
76 constants => [%myconstants], # define constants for exporting
77 default => [ keys %myconstants ], # export everything in %myconstants by default
78);
79```
80
81# DESCRIPTION
82
83This module allows you to declare constants that can be exported to
84other modules.
85
86To declare constants, simply group then into export tags:
87
88```perl
89package MyApp::Constants;
90
91use Const::Exporter
92
93 tag_a => [
94 'foo' => 1,
95 'bar' => 2,
96 ],
97
98 tag_b => [
99 'baz' => 3,
100 'bar',
101 ],
102
103 default => [
104 'foo',
105 ];
106```
107
108Constants in the `default` tag are exported by default (that is, they
109are added to the `@EXPORTS` array).
110
111When a constant is already defined in a previous tag, then no value is
112specified for it. (For example, `bar` in `tab_b` above.) If you do
113give a value, [Const::Exporter](https://metacpan.org/pod/Const::Exporter) will assume it's another symbol.
114
115Your module can include multiple calls to `use Const::Exporter`, so
116that you can reference constants in other expressions, e.g.
117
118```perl
119use Const::Exporter
120
121 tag => [
122 '$zero' => 0,
123 ];
124
125use Const::Exporter
126
127 tag => [
128 '$one' => 1 + $zero,
129 ];
130```
131
132or even something more complex:
133
134```perl
135use Const::Exporter
136
137 http_ports => [
138 'HTTP' => 80,
139 'HTTP_ALT' => 8080,
140 'HTTPS' => 443,
141 ];
142
143use Const::Exporter
144
145 http_ports => [
146 '@HTTP_PORTS' => [ HTTP, HTTP_ALT, HTTPS ],
147 ];
148```
149
150Constants can include traditional [constant](https://metacpan.org/pod/constant) symbols, as well as
151scalars, arrays or hashes.
152
153Constants can include values defined elsewhere in the code, e.g.
154
155```perl
156our $foo;
157
158BEGIN {
159 $foo = calculate_value_for_constant();
160}
161
162use Const::Exporter
163
164 tag => [ '$foo' ];
165```
166
167Note that this will make the symbol read-only. You don't need to
168explicitly declare it as such.
169
170Enumerated constants are also supported:
171
172```perl
173use Const::Exporter
174
175 tag => [
176
177 [qw/ foo bar baz /] => 1,
178
179 ];
180```
181
182will define the symbols `foo` (1), `bar` (2) and `baz` (3).
183
184You can also specify a list of numbers, if you want to skip values:
185
186```perl
187use Const::Exporter
188
189 tag => [
190
191 [qw/ foo bar baz /] => [1, 4],
192
193 ];
194```
195
196will define the symbols `foo` (1), `bar` (4) and `baz` (5).
197
198You can even specify string values:
199
200```perl
201use Const::Exporter
202
203 tag => [
204
205 [qw/ foo bar baz /] => [qw/ feh meh neh /],
206
207 ];
208```
209
210however, this is equivalent to
211
212```perl
213use Const::Exporter
214
215 tag => [
216 'foo' => 'feh',
217 'bar' => 'meh',
218 'baz' => 'neh',
219 ];
220```
221
222Objects are also supported,
223
224```perl
225use Const::Exporter
226
227 tag => [
228 '$foo' => Something->new( 123 ),
229 ];
230```
231
232## Export Tags
233
234By default, all symbols are exportable (in `@EXPORT_OK`.)
235
236The `:default` tag is the same as not specifying any exports.
237
238The `:all` tag exports all symbols.
239
240# KNOWN ISSUES
241
242## Support for older Perl versions
243
244This module requires Perl v5.10 or newer.
245
246Pull requests to support older versions of Perl are welcome. See
247["SOURCE"](#source).
248
249## Exporting Functions
250
251[Const::Exporter](https://metacpan.org/pod/Const::Exporter) is not intended for use with modules that also
252export functions.
253
254There are workarounds that you can use, such as getting
255[Const::Exporter](https://metacpan.org/pod/Const::Exporter) to export your functions, or munging `@EXPORT`
256etc. separately, but these are not supported and changes in the
257future my break our code.
258
259## Mixing POD with Tags
260
261The following code is a syntax error, at least with some versions of
262Perl:
263
264```perl
265use Const::Exporter
266
267=head2 a
268
269=cut
270
271 a => [ foo => 1 ],
272
273=head2 b
274
275=cut
276
277 b => [ bar => 2 ];
278```
279
280If you want to mix POD with your declarations, use multiple use lines,
281e.g.
282
283```perl
284=head2 a
285
286=cut
287
288use Const::Exporter
289 a => [ foo => 1 ];
290
291=head2 b
292
293=cut
294
295use Const::Exporter
296 b => [ bar => 2 ];
297```
298
299# SEE ALSO
300
301See [Exporter](https://metacpan.org/pod/Exporter) for a discussion of export tags.
302
303## Similar Modules
304
305- [Exporter::Constants](https://metacpan.org/pod/Exporter::Constants)
306
307 This module only allows you to declare function symbol constants, akin
308 to the [constant](https://metacpan.org/pod/constant) module, without tags.
309
310- [Constant::Exporter](https://metacpan.org/pod/Constant::Exporter)
311
312 This module only allows you to declare function symbol constants, akin
313 to the [constant](https://metacpan.org/pod/constant) module, although you can specify tags.
314
315- [Constant::Export::Lazy](https://metacpan.org/pod/Constant::Export::Lazy)
316
317 This module only allows you to declare function symbol constants, akin
318 to the [constant](https://metacpan.org/pod/constant) module by defining functions that are only called
319 as needed. The interface is rather complex.
320
321- [Const::Fast::Exporter](https://metacpan.org/pod/Const::Fast::Exporter)
322
323 This module will export all constants declared in the package's
324 namespace.
325
326# SOURCE
327
328The development version is on github at [https://github.com/robrwo/Const-Exporter](https://github.com/robrwo/Const-Exporter)
329and may be cloned from [git://github.com/robrwo/Const-Exporter.git](git://github.com/robrwo/Const-Exporter.git)
330
331# BUGS
332
333Please report any bugs or feature requests on the bugtracker website
334[https://github.com/robrwo/Const-Exporter/issues](https://github.com/robrwo/Const-Exporter/issues)
335
336When submitting a bug or request, please include a test-file or a
337patch to an existing test-file that illustrates the bug or desired
338feature.
339
340# AUTHOR
341
342Robert Rothenberg <rrwo@cpan.org>
343
344# CONTRIBUTORS
345
346- Slaven Rezić <slaven@rezic.de>
347- B. Estrade <estrabd@cpan.org>
348
349# COPYRIGHT AND LICENSE
350
351This software is Copyright (c) 2014-2021 by Robert Rothenberg.
352
353This is free software, licensed under:
354
355```
356The Artistic License 2.0 (GPL Compatible)
357```
358