File Coverage

blib/lib/HTTP/Status/Const.pm
Criterion Covered Total %
statement 37 37 100.0
branch 2 2 100.0
condition n/a
subroutine 8 8 100.0
pod n/a
total 47 47 100.0


line stmt bran cond sub pod time code
1             package HTTP::Status::Const;
2              
3 1     1   18632 use v5.10.1;
  1         4  
  1         66  
4              
5 1     1   7 use strict;
  1         2  
  1         62  
6 1     1   7 use warnings;
  1         8  
  1         49  
7              
8 1     1   736 use version; our $VERSION = version->declare('v0.3.0');
  1         1724  
  1         7  
9              
10 1     1   585 use Const::Exporter;
  1         12916  
  1         6  
11 1     1   586 use HTTP::Status qw/ :is status_message /;
  1         2737  
  1         247  
12 1     1   6 use Package::Stash;
  1         1  
  1         164  
13              
14             # RECOMMEND PREREQ: Package::Stash::XS 0
15              
16             =head1 NAME
17              
18             HTTP::Status::Const - interpolable HTTP status constants
19              
20             =for readme plugin version
21              
22             =head1 SYNOPSIS
23              
24             use HTTP::Status::Const;
25              
26             ...
27              
28             $response->status( $HTTP_NOT_FOUND );
29              
30             ...
31              
32             my %handlers = (
33             $HTTP_OK => sub { ... },
34             $HTTP_CREATED => sub { ... },
35             ...
36             );
37              
38             =head1 DESCRIPTION
39              
40             This module is basically a wrapper around L that allows
41             you to use the constants as read-only scalar variables instead of
42             function names.
43              
44             This means the constants can be used in contexts where you need
45             interpolated variables, such as hash keys or in strings.
46              
47             =head2 Do I really need this?
48              
49             No. You can get interpolated constants already, with some ugly syntax:
50              
51             my %handlers = (
52             HTTP_OK() => sub { ... },
53             );
54              
55             or
56              
57             "Status code ${ \HTTP_OK }"
58              
59             So all this module gives you is some stylistic convenience, at the
60             expense of additional dependencies (although ones that may be used
61             by other modules).
62              
63             =begin :readme
64              
65             =head1 INSTALLATION
66              
67             See
68             L.
69              
70             =for readme plugin requires heading-level=2 title="Required Modules"
71              
72             =for readme plugin changes
73              
74             =end :readme
75              
76             =head1 EXPORTS
77              
78             By default, only the HTTP constants are exported.
79              
80             For convenience, the tags from L are supported so that
81             the C<:is> and C functions are exported.
82              
83             =head1 SEE ALSO
84              
85             L
86              
87             =head1 AUTHOR
88              
89             Robert Rothenberg, C<< >>
90              
91             =head2 Acknowledgements
92              
93             Several people who pointed out that this module is unnecessary.
94             (Yes, it's written to scratch an itch.)
95              
96             =head1 LICENSE AND COPYRIGHT
97              
98             Copyright 2014-2015 Robert Rothenberg.
99              
100             This program is free software; you can redistribute it and/or modify it
101             under the terms of the the Artistic License (2.0). You may obtain a
102             copy of the full license at:
103              
104             L
105              
106             =for readme stop
107              
108             Any use, modification, and distribution of the Standard or Modified
109             Versions is governed by this Artistic License. By using, modifying or
110             distributing the Package, you accept this license. Do not use, modify,
111             or distribute the Package, if you do not accept this license.
112              
113             If your Modified Version has been derived from a Modified Version made
114             by someone other than you, you are nevertheless required to ensure that
115             your Modified Version complies with the requirements of this license.
116              
117             This license does not grant you the right to use any trademark, service
118             mark, tradename, or logo of the Copyright Holder.
119              
120             This license includes the non-exclusive, worldwide, free-of-charge
121             patent license to make, have made, use, offer to sell, sell, import and
122             otherwise transfer the Package with respect to any patent claims
123             licensable by the Copyright Holder that are necessarily infringed by the
124             Package. If you institute patent litigation (including a cross-claim or
125             counterclaim) against any party alleging that the Package constitutes
126             direct or contributory patent infringement, then this Artistic License
127             to you shall terminate on the date that such litigation is filed.
128              
129             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
130             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
131             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
132             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
133             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
134             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
135             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
136             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
137              
138             =for readme continue
139              
140             =cut
141              
142             BEGIN {
143 1     1   11 my $stash = Package::Stash->new('HTTP::Status');
144 1         41 my $syms = $stash->get_all_symbols('CODE');
145              
146 1         2 my %defaults;
147              
148 1         1 foreach my $sym ( keys %{$syms} ) {
  1         11  
149 124 100       254 next unless $sym =~ /^HTTP_/;
150 58         38 my $val = &{ $syms->{$sym} };
  58         74  
151 58         100 $defaults{ '$' . $sym } = $val;
152             }
153              
154             Const::Exporter->import(
155 1         30 constants => [%defaults],
156             default => [ keys %defaults ],
157             );
158              
159 1         4318 $EXPORT_TAGS{is} = $HTTP::Status::EXPORT_TAGS{is};
160              
161 1         3 push @EXPORT_OK, 'status_message', @{ $EXPORT_TAGS{is} };
  1         2  
162 1         2 push @{ $EXPORT_TAGS{all} }, 'status_message', @{ $EXPORT_TAGS{is} };
  1         3  
  1         101  
163             }
164              
165             1;