File Coverage

blib/lib/App/yajg/Hooks.pm
Criterion Covered Total %
statement 53 53 100.0
branch 16 24 66.6
condition 6 10 60.0
subroutine 15 15 100.0
pod 0 5 0.0
total 90 107 84.1


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