File Coverage

blib/lib/URI/Query/FromHash.pm
Criterion Covered Total %
statement 31 32 96.8
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 43 44 97.7


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