File Coverage

blib/lib/Data/Decode/Encode/Guess.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition 2 5 40.0
subroutine 7 7 100.0
pod 2 2 100.0
total 39 42 92.8


line stmt bran cond sub pod time code
1             # $Id: /mirror/perl/Data-Decode/trunk/lib/Data/Decode/Encode/Guess.pm 4834 2007-11-03T09:22:42.139028Z daisuke $
2             #
3             # Copyright (c) 2007 Daisuke Maki
4             # All rights reserved.
5              
6             package Data::Decode::Encode::Guess;
7 4     4   6785 use strict;
  4         10  
  4         162  
8 4     4   21 use warnings;
  4         9  
  4         135  
9 4     4   20 use base qw(Class::Accessor::Fast);
  4         6  
  4         348  
10 4     4   4162 use Encode();
  4         97781  
  4         9193  
11 4     4   5005 use Encode::Guess();
  4         28600  
  4         1329  
12              
13             __PACKAGE__->mk_accessors($_) for qw(encodings);
14              
15             sub new
16             {
17 4     4 1 43 my $class = shift;
18 4         17 my %args = @_;
19 4   50     20 $args{encodings} ||= [];
20 4         58 $class->SUPER::new(\%args);
21             }
22              
23             sub decode
24             {
25 13     13 1 31 my ($self, $decoder, $string, $hints) = @_;
26              
27 13         22 local $Encode::Guess::NoUTFAutoGuess = 1;
28 13         57 my $guess = Encode::Guess::guess_encoding(
29             $string,
30 13         17 @{ $self->encodings }
31             );
32              
33 13 100       54099 if (! ref $guess) {
34 3         29 Data::Decode::Exception::Deferred->throw($guess);
35             }
36              
37 10   33     21 return eval { $guess->decode( $string ) } ||
38             Data::Decode::Exception::Deferred->throw("Failed to decode string from " . $guess->name . ": $@")
39             ;
40             }
41              
42             1;
43              
44             __END__