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   15403 use v5.10.0;
  1         3  
  1         36  
4              
5 1     1   4 use strict;
  1         2  
  1         28  
6 1     1   3 use warnings;
  1         4  
  1         26  
7              
8 1     1   470 use version 0.77; our $VERSION = version->declare('v0.2.2');
  1         1390  
  1         6  
9              
10 1     1   591 use Const::Exporter;
  1         10385  
  1         5  
11 1     1   527 use HTTP::Status qw/ :is status_message /;
  1         2800  
  1         204  
12 1     1   6 use Package::Stash;
  1         1  
  1         152  
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             See the L documentation for more details on the POD
66             syntax that this module recognizes.
67              
68             See L for command-line usage.
69              
70             =head1 INSTALLATION
71              
72             See
73             L.
74              
75             =for readme plugin requires heading-level=2 title="Required Modules"
76              
77             =for readme plugin changes
78              
79             =end :readme
80              
81             =head1 EXPORTS
82              
83             By default, only the HTTP constants are exported.
84              
85             For convenience, the tags from L are supported so that
86             the C<:is> and C functions are exported.
87              
88             =head1 SEE ALSO
89              
90             L
91              
92             =head1 AUTHOR
93              
94             Robert Rothenberg, C<< >>
95              
96             =head2 Acknowledgements
97              
98             Several people who pointed out that this module is unnecessary.
99             (Yes, it's written to scratch an itch.)
100              
101             =head1 LICENSE AND COPYRIGHT
102              
103             Copyright 2014 Robert Rothenberg.
104              
105             This program is free software; you can redistribute it and/or modify it
106             under the terms of the the Artistic License (2.0). You may obtain a
107             copy of the full license at:
108              
109             L
110              
111             =for readme stop
112              
113             Any use, modification, and distribution of the Standard or Modified
114             Versions is governed by this Artistic License. By using, modifying or
115             distributing the Package, you accept this license. Do not use, modify,
116             or distribute the Package, if you do not accept this license.
117              
118             If your Modified Version has been derived from a Modified Version made
119             by someone other than you, you are nevertheless required to ensure that
120             your Modified Version complies with the requirements of this license.
121              
122             This license does not grant you the right to use any trademark, service
123             mark, tradename, or logo of the Copyright Holder.
124              
125             This license includes the non-exclusive, worldwide, free-of-charge
126             patent license to make, have made, use, offer to sell, sell, import and
127             otherwise transfer the Package with respect to any patent claims
128             licensable by the Copyright Holder that are necessarily infringed by the
129             Package. If you institute patent litigation (including a cross-claim or
130             counterclaim) against any party alleging that the Package constitutes
131             direct or contributory patent infringement, then this Artistic License
132             to you shall terminate on the date that such litigation is filed.
133              
134             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
135             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
136             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
137             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
138             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
139             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
140             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
141             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
142              
143             =for readme continue
144              
145             =cut
146              
147             BEGIN {
148 1     1   12 my $stash = Package::Stash->new('HTTP::Status');
149 1         39 my $syms = $stash->get_all_symbols('CODE');
150              
151 1         2 my %defaults;
152              
153 1         1 foreach my $sym ( keys %{$syms} ) {
  1         10  
154 124 100       197 next unless $sym =~ /^HTTP_/;
155 58         31 my $val = &{ $syms->{$sym} };
  58         79  
156 58         102 $defaults{ '$' . $sym } = $val;
157             }
158              
159             Const::Exporter->import(
160 1         28 constants => [%defaults],
161             default => [ keys %defaults ],
162             );
163              
164 1         4439 $EXPORT_TAGS{is} = $HTTP::Status::EXPORT_TAGS{is};
165              
166 1         2 push @EXPORT_OK, 'status_message', @{ $EXPORT_TAGS{is} };
  1         3  
167 1         1 push @{ $EXPORT_TAGS{all} }, 'status_message', @{ $EXPORT_TAGS{is} };
  1         2  
  1         69  
168             }
169              
170             1;