File Coverage

blib/lib/URI/Query/FromHash.pm
Criterion Covered Total %
statement 31 32 96.8
branch 4 4 100.0
condition 2 2 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 45 46 97.8


line stmt bran cond sub pod time code
1             package URI::Query::FromHash 0.006;
2              
3 2     2   30292 use strict;
  2         4  
  2         52  
4 2     2   8 use warnings;
  2         4  
  2         121  
5              
6             my %escapes;
7             $escapes{+chr} = sprintf '%%%02X', $_ for 0..255;
8              
9             sub import {
10 2     2   11 no strict 'refs';
  2         6  
  2         576  
11              
12 4     4   1336 *{ caller . '::hash2query' } = \&hash2query;
  4         1229  
13             }
14              
15             sub hash2query(+%) {
16 12 100   12 1 76 return '' if 'HASH' ne ref( my $hash = $_[0] );
17              
18 10         19 my $q = '';
19              
20 10         36 for my $k ( sort keys %$hash ) {
21 11         28 my $v = $hash->{$k};
22              
23 11         25 $k =~ s|([;/?:@&=+,\$\[\]%\\])|$escapes{$1}|g;
24 11         21 $k =~ y| |+|;
25              
26 11 100       29 for ( ref $v ? @$v : $v ) {
27             # Avoid modifying the original.
28 15   100     38 my $v = $_ // '';
29              
30 15         22 $v =~ s|([;/?:@&=+,\$\[\]%\\])|$escapes{$1}|g;
31 15         23 $v =~ y| |+|;
32              
33 15         35 $q .= "$k=$v&";
34             }
35             }
36              
37             # Trim off the last "&".
38 10         25 substr $q, -1, 1, '';
39              
40 10         34 utf8::encode $q;
41              
42 10         18 $q =~ s|([^\Q;/?:@&=+,\$\[\]%-_.!~*'()\EA-Za-z0-9])|@escapes{ split //, $1 }|eg;
  0         0  
43              
44 10         55 $q;
45             }
46              
47             sub unimport {
48 2     2   12 no strict 'refs';
  2         5  
  2         89  
49              
50 4     4   5354 delete ${ caller . '::' }{hash2query};
  4         220  
51             }
52              
53             1;