Skip to content

Commit 82aa855

Browse files
neilbsimbabque
authored andcommitted
Added status_constant_name() and clarified what status_message() returns
1 parent c6e6973 commit 82aa855

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Revision history for HTTP-Message
22

33
{{$NEXT}}
44
- Remove Travis config (GH#180) (Olaf Alders)
5+
- Added status_constant_name() which maps status code
6+
to the name of the corresponding constant. (GH#160) (Neil Bowers)
7+
- Updated the doc for status_message() to clarify that it
8+
returns "Not Found" and not "HTTP_NOT_FOUND". (GH#160) (Neil Bowers)
59

610
6.38 2022-10-06 21:48:18Z
711
- Replace "base" with "parent" (GH#176) (James Raspass)

lib/HTTP/Status.pm

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ our $VERSION = '6.39';
88
use Exporter 5.57 'import';
99

1010
our @EXPORT = qw(is_info is_success is_redirect is_error status_message);
11-
our @EXPORT_OK = qw(is_client_error is_server_error is_cacheable_by_default);
11+
our @EXPORT_OK = qw(is_client_error is_server_error is_cacheable_by_default status_constant_name);
1212

1313
# Note also addition of mnemonics to @EXPORT below
1414

@@ -90,6 +90,8 @@ my %StatusCode = (
9090
511 => 'Network Authentication Required', # RFC 6585: Additional Codes
9191
);
9292

93+
my %StatusCodeName;
94+
9395
# keep some unofficial codes that used to be in this distribution
9496
%StatusCode = (
9597
%StatusCode,
@@ -104,10 +106,12 @@ while (($code, $message) = each %StatusCode) {
104106
# create mnemonic subroutines
105107
$message =~ s/I'm/I am/;
106108
$message =~ tr/a-z \-/A-Z__/;
107-
$mnemonicCode .= "sub HTTP_$message () { $code }\n";
109+
my $constant_name = "HTTP_".$message;
110+
$mnemonicCode .= "sub $constant_name () { $code }\n";
108111
$mnemonicCode .= "*RC_$message = \\&HTTP_$message;\n"; # legacy
109112
$mnemonicCode .= "push(\@EXPORT_OK, 'HTTP_$message');\n";
110113
$mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
114+
$StatusCodeName{$code} = $constant_name
111115
}
112116
eval $mnemonicCode; # only one eval for speed
113117
die if $@;
@@ -139,6 +143,9 @@ our %EXPORT_TAGS = (
139143

140144

141145
sub status_message ($) { $StatusCode{$_[0]}; }
146+
sub status_constant_name ($) {
147+
exists($StatusCodeName{$_[0]}) ? $StatusCodeName{$_[0]} : undef;
148+
}
142149

143150
sub is_info ($) { $_[0] && $_[0] >= 100 && $_[0] < 200; }
144151
sub is_success ($) { $_[0] && $_[0] >= 200 && $_[0] < 300; }
@@ -273,7 +280,20 @@ the classification functions.
273280
274281
The status_message() function will translate status codes to human
275282
readable strings. The string is the same as found in the constant
276-
names above. If the $code is not registered in the L<list of IANA HTTP Status
283+
names above.
284+
For example, C<status_message(303)> will return C<"Not Found">.
285+
286+
If the $code is not registered in the L<list of IANA HTTP Status
287+
Codes|https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>
288+
then C<undef> is returned.
289+
290+
=item status_constant_name( $code )
291+
292+
The status_constant_name() function will translate a status code
293+
to a string which has the name of the constant for that status code.
294+
For example, C<status_constant_name(404)> will return C<"HTTP_NOT_FOUND">.
295+
296+
If the C<$code> is not registered in the L<list of IANA HTTP Status
277297
Codes|https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>
278298
then C<undef> is returned.
279299

t/status.t

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use strict;
22
use warnings;
33

44
use Test::More;
5-
plan tests => 49;
5+
plan tests => 52;
66

7-
use HTTP::Status qw(:constants :is status_message);
7+
use HTTP::Status qw(:constants :is status_message status_constant_name);
88

99
is(HTTP_OK, 200);
1010

@@ -52,3 +52,7 @@ ok(is_cacheable_by_default($_),
5252
ok(!is_cacheable_by_default($_),
5353
"... is not cacheable [$_] " . status_message($_)
5454
) for (100,201,302,400,500);
55+
56+
is(status_constant_name(HTTP_OK), "HTTP_OK");
57+
is(status_constant_name(404), "HTTP_NOT_FOUND");
58+
is(status_constant_name(999), undef);

0 commit comments

Comments
 (0)