@@ -8,7 +8,7 @@ our $VERSION = '6.39';
88use Exporter 5.57 ' import' ;
99
1010our @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}
112116eval $mnemonicCode ; # only one eval for speed
113117die if $@ ;
@@ -139,6 +143,9 @@ our %EXPORT_TAGS = (
139143
140144
141145sub status_message ($) { $StatusCode {$_ [0]}; }
146+ sub status_constant_name ($) {
147+ exists ($StatusCodeName {$_ [0]}) ? $StatusCodeName {$_ [0]} : undef ;
148+ }
142149
143150sub is_info ($) { $_ [0] && $_ [0] >= 100 && $_ [0] < 200; }
144151sub is_success ($) { $_ [0] && $_ [0] >= 200 && $_ [0] < 300; }
@@ -273,7 +280,20 @@ the classification functions.
273280
274281The status_message() function will translate status codes to human
275282readable 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
277297Codes|https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>
278298then C<undef > is returned.
279299
0 commit comments