File Coverage

blib/lib/App/yajg/Hooks.pm
Criterion Covered Total %
statement 56 56 100.0
branch 17 24 70.8
condition 9 13 69.2
subroutine 16 16 100.0
pod 0 5 0.0
total 98 114 85.9


line stmt bran cond sub pod time code
1             package App::yajg::Hooks;
2              
3             # Various hooks to modify data that is not array or hash ref
4             # The refs to subs from this package are used at App::yajg::modify_data
5              
6 1     1   416 use 5.014000;
  1         2  
7 1     1   3 use strict;
  1         0  
  1         16  
8 1     1   3 use warnings;
  1         1  
  1         17  
9 1     1   3 use utf8;
  1         1  
  1         4  
10              
11 1     1   18 use Scalar::Util 'blessed';
  1         2  
  1         470  
12              
13             sub _is_bool ($) {
14 36 100 66 36   16518 blessed $_[0] and (
      66        
15             $_[0]->isa('Types::Serialiser::BooleanBase')
16             or $_[0]->isa('JSON::PP::Boolean')
17             or $_[0]->isa('JSON::XS::Boolean')
18             )
19             }
20              
21             sub boolean_to_scalar_ref {
22 2 50   2 0 732 return unless _is_bool $_[0];
23 2 100       26 $_[0] = $_[0]
24             ? \(my $t = 1)
25             : \(my $f = 0);
26             }
27              
28             sub boolean_to_int {
29 2 50   2 0 1410 return unless _is_bool $_[0];
30 2         4 $_[0] = int(!!$_[0]);
31             }
32              
33             sub boolean_to_str {
34 2 50   2 0 1098 return unless _is_bool $_[0];
35 2 100       4 $_[0] = $_[0]
36             ? 'true'
37             : 'false';
38             }
39              
40             sub _decode_uri ($) {
41 39   100 39   66 local $_ = shift // return undef;
42 31         24 tr/\+/ /;
43 31         40 s/\%([a-f\d]{2})/pack("C",hex($1))/ieg;
  10         41  
44 31 50       73 utf8::decode($_) unless utf8::is_utf8($_);
45 31         56 return $_;
46             }
47              
48             # Do not use URI for increase speed
49             sub uri_parse {
50 6 50 33 6 0 4185 return unless defined $_[0] and not ref $_[0];
51 6         5 my %uri;
52             # From URI::Split::uri_split
53 6         53 @uri{qw/scheme host path query fragment/} =
54             $_[0] =~ m,(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?,;
55 6 100       18 return unless defined $uri{'host'};
56 4         5 $uri{'uri'} = $_[0];
57             $uri{'path'} = [
58 4         15 map { _decode_uri($_) } split '/' => $uri{'path'} =~ s,^/,,r
  11         13  
59             ];
60             # From URI::_query::query_form
61             $uri{'query'} = {
62 24         24 map { _decode_uri($_) }
63 12 100       31 map { /=/ ? split(/=/, $_, 2) : ($_ => undef) }
64 4   100     22 split /[&;]/ => ($uri{'query'} // '')
65             };
66 4         10 $uri{'fragment'} = _decode_uri($uri{'fragment'});
67 4         9 $_[0] = \%uri;
68             }
69              
70             sub make_code_hook ($) {
71 1     1 0 1461 my $code = shift;
72 1 50       4 utf8::decode($code) unless utf8::is_utf8($code);
73             return sub {
74 1     1   218 local $_ = $_[0];
75 1     1   4 { no strict; no warnings; eval $code; }
  1     1   1  
  1     1   16  
  1         2  
  1         1  
  1         74  
  1         4  
  1         2  
  1         9  
  1         1  
  1         59  
76 1 50       16578 warn "$@\n" if $@;
77 1         4 $_[0] = $_;
78 1         4 };
79             }
80              
81             1;