QQWry/Decoded.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 65 CC 8 SCAR 30.0
CC
8
Cov
0%
query
42.0
new
12.0
encoding
2.0
CRAP 20.0
1 package IP::QQWry::Decoded;
2  
3 use 5.008;
4 use warnings;
5 use strict;
6 use base 'IP::QQWry';
7 use Encode;
8  
9 sub new {
10 my ( $class, $db, $encoding ) = @_;
11 my $self = {};
12 bless $self, $class;
13 if ($db) {
14 $self->set_db($db);
15 }
16  
17 if ( $encoding ) {
18 $self->{encoding} = $encoding;
19 }
20  
21 return $self;
22 }
23  
24 sub encoding {
25 return $_[0]->{encoding};
26 }
27  
28 sub query {
29 my $self = shift;
30 my @ret = $self->SUPER::query(@_);
31 return unless @ret;
32 my @converted;
33  
34 if ($self->encoding) {
35 @converted = map { decode( $self->encoding, $_ ) } @ret;
36 }
37 else {
38 # then it's either gbk or big5
39 eval {
40 @converted = map { decode( 'gbk', $_ ) } @ret;
41 };
42  
43 if ($@) {
44 @converted = map { decode( 'big5', $_ ) } @ret;
45 }
46 }
47  
48 die "failed to decode" unless @converted;
49 return wantarray ? @converted : join '', @converted;
50 }
51  
52 1;
53  
54 __END__