Skip to content

Commit eae45a4

Browse files
committed
Use lexicals to cache encoding values
as suggested by Encode docs
1 parent 40ba548 commit eae45a4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/MetaCPAN/Web/Model/API.pm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,23 @@ sub request {
7474
return $req;
7575
}
7676

77+
my $encoding = Encode::find_encoding('utf-8-strict')
78+
or warn 'UTF-8 Encoding object not found';
79+
my $encode_check = ( Encode::FB_CROAK | Encode::LEAVE_SRC );
80+
7781
sub raw_api_response {
7882
my ($self, $data) = @_;
7983

84+
# will http_response ever return undef or a blessed object?
85+
$data = '' if ! defined $data; # define
86+
$data .= '' if ref $data; # stringify
87+
8088
# we have to assume an encoding; doing nothing is like assuming latin1
8189
# we'll probably have the least number of issues if we assume utf8
8290
local $@;
8391
eval {
8492
# decode so the template doesn't double-encode and return mojibake
85-
$data = Encode::decode('UTF-8', $data, Encode::FB_CROAK);
93+
$data &&= $encoding->decode( $data, $encode_check );
8694
};
8795
warn $@ if $@;
8896

0 commit comments

Comments
 (0)