| 1 | package exported::constants; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use vars '$VERSION'; $VERSION = '1.0'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Carp qw/ carp /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=4 SCAR=14.1
| |||||||||||
| 11 | 1 | my ($class, @args) = @_; | |||||||||
| 12 | |||||||||||
| 13 | 1 | my $pkg = caller(); | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 0 | ▶ | push @{$pkg.'::ISA'}, 'Exporter' unless grep { $_ eq 'Exporter' } @{$pkg.'::ISA'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | while (@args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 2 | ▶ | carp "Last constant $args[0] is missing a definition" unless @args >= 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 2 | my ($name, $value) = splice @args, 0, 2; | |||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | *{$pkg.'::'.$name} = sub () { $value }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||
| 25 | |||||||||||
| 26 | 2 | push @{$pkg.'::EXPORT'}, $name; | |||||||||
| 27 | } | ||||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 1 | 1; | |||||||||
| 31 | |||||||||||
| 32 | =head1 NAME | ||||||||||
| 33 | |||||||||||
| 34 | exported::constants - Declare constants and export them automatically | ||||||||||
| 35 | |||||||||||
| 36 | =head1 SYNOPSIS | ||||||||||
| 37 | |||||||||||
| 38 | package MyProg::Constants; | ||||||||||
| 39 | |||||||||||
| 40 | use exported::constants | ||||||||||
| 41 | USER_TYPE_USER => 'U', | ||||||||||
| 42 | USER_TYPE_APPLICATION => 'A', | ||||||||||
| 43 | USER_TYPE_ROBOT => 'B', | ||||||||||
| 44 | ; | ||||||||||
| 45 | |||||||||||
| 46 | package MyProg::App; | ||||||||||
| 47 | |||||||||||
| 48 | use MyProg::Constants; | ||||||||||
| 49 | |||||||||||
| 50 | my @real_users = $users->search({ user_type => USER_TYPE_USER, }); | ||||||||||
| 51 | |||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||
| 53 | |||||||||||
| 54 | This is a boilerplate-removal module for creating modules of just constants in your program. | ||||||||||
| 55 | This is useful if you have a lot of magic numbers you want to eliminate, | ||||||||||
| 56 | especially things that show up in database schemas or APIs that you want to re-use across multiple modules. | ||||||||||
| 57 | |||||||||||
| 58 | It's pretty simple to use; | ||||||||||
| 59 | just say | ||||||||||
| 60 | |||||||||||
| 61 | use exported::constants | ||||||||||
| 62 | CONSTANT1 => $value1, | ||||||||||
| 63 | CONSTANT2 => $value2, | ||||||||||
| 64 | ; | ||||||||||
| 65 | |||||||||||
| 66 | and your package is automatically an exporter, | ||||||||||
| 67 | and automatically exports (by default) all the constants listed. | ||||||||||
| 68 | |||||||||||
| 69 | =head1 RESTRICTIONS | ||||||||||
| 70 | |||||||||||
| 71 | =over | ||||||||||
| 72 | |||||||||||
| 73 | =item * List constants don't work, because C<exported::constants> is intended to always create multiple constants in a single invocation. | ||||||||||
| 74 | |||||||||||
| 75 | =item * This module always works using C<@EXPORT> in L<Exporter>; this is unfortunate for developers who want to explicitly import all their constants. | ||||||||||
| 76 | |||||||||||
| 77 | =back | ||||||||||
| 78 | |||||||||||
| 79 | =head1 SEE ALSO | ||||||||||
| 80 | |||||||||||
| 81 | =over | ||||||||||
| 82 | |||||||||||
| 83 | =item * L<constant> | ||||||||||
| 84 | |||||||||||
| 85 | =item * L<Exporter> | ||||||||||
| 86 | |||||||||||
| 87 | =back | ||||||||||
| 88 | |||||||||||
| 89 | =head1 AUTHOR | ||||||||||
| 90 | |||||||||||
| 91 | Jonathan Cast <jonathanccast@fastmail.fm> | ||||||||||
| 92 | |||||||||||
| 93 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 94 | |||||||||||
| 95 | Copyright 2017 Jonathan Cast | ||||||||||
| 96 | |||||||||||
| 97 | Licensed under the Apache License, Version 2.0 (the "License"). | ||||||||||