File Coverage

blib/lib/App/yajg/Hooks.pm
Criterion Covered Total %
statement 55 55 100.0
branch 15 22 68.1
condition 5 7 71.4
subroutine 15 15 100.0
pod 0 5 0.0
total 90 104 86.5


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   443 use 5.014000;
  1         4  
7 1     1   3 use strict;
  1         0  
  1         15  
8 1     1   2 use warnings;
  1         1  
  1         16  
9 1     1   2 use utf8;
  1         1  
  1         4  
10              
11 1     1   16 use JSON qw();
  1         1  
  1         458  
12              
13             sub boolean_to_scalar_ref {
14 2 50   2 0 1088 return unless JSON::is_bool($_[0]);
15 2 100       38 $_[0] = $_[0]
16             ? \(my $t = 1)
17             : \(my $f = 0);
18             }
19              
20             sub boolean_to_int {
21 2 50   2 0 2093 return unless JSON::is_bool($_[0]);
22 2         11 $_[0] = int(!!$_[0]);
23             }
24              
25             sub boolean_to_str {
26 2 50   2 0 1515 return unless JSON::is_bool($_[0]);
27 2 100       14 $_[0] = $_[0]
28             ? 'true'
29             : 'false';
30             }
31              
32             sub _decode_uri ($) {
33 39   100 39   53 local $_ = shift // return undef;
34 31         22 tr/\+/ /;
35 31         30 s/\%([a-f\d]{2})/pack("C",hex($1))/ieg;
  10         29  
36 31 50       54 utf8::decode($_) unless utf8::is_utf8($_);
37 31         41 return $_;
38             }
39              
40             # Do not use URI for increase speed
41             sub uri_parse {
42 6 50 33 6 0 4295 return unless defined $_[0] and not ref $_[0];
43 6         5 my %uri;
44             # From URI::Split::uri_split
45 6         45 @uri{qw/scheme host path query fragment/} =
46             $_[0] =~ m,(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?,;
47 6 100       15 return unless defined $uri{'host'};
48 4         5 $uri{'uri'} = $_[0];
49             $uri{'path'} = [
50 4         13 map { _decode_uri($_) } split '/' => $uri{'path'} =~ s,^/,,r
  11         10  
51             ];
52             # From URI::_query::query_form
53             $uri{'query'} = {
54 24         19 map { _decode_uri($_) }
55 12 100       23 map { /=/ ? split(/=/, $_, 2) : ($_ => undef) }
56 4   100     17 split /[&;]/ => ($uri{'query'} // '')
57             };
58 4         8 $uri{'fragment'} = _decode_uri($uri{'fragment'});
59 4         8 $_[0] = \%uri;
60             }
61              
62             sub make_code_hook ($) {
63 1     1 0 1602 my $code = shift;
64 1 50       4 utf8::decode($code) unless utf8::is_utf8($code);
65             return sub {
66 1     1   324 local $_ = $_[0];
67 1     1   4 { no strict; no warnings; eval $code; }
  1     1   1  
  1     1   20  
  1         3  
  1         1  
  1         82  
  1         5  
  1         1  
  1         8  
  1         1  
  1         57  
68 1 50       16969 warn "$@\n" if $@;
69 1         3 $_[0] = $_;
70 1         4 };
71             }
72              
73             1;