File Coverage

blib/lib/PXF/Util.pm
Criterion Covered Total %
statement 70 70 100.0
branch 2 2 100.0
condition n/a
subroutine 18 18 100.0
pod 1 15 6.6
total 91 105 86.6


line stmt bran cond sub pod time code
1 12     12   438824 use v5.36;
  12         67  
2             package PXF::Util {
3 12     12   6140 use MIME::Base64 ();
  12         11204  
  12         372  
4 12     12   99 use Digest::MD5 ();
  12         52  
  12         7087  
5              
6             # MD5 digests, url-encoded
7 42     42 0 8678 sub md5_ubase64 ($str) { Digest::MD5::md5_base64($str) =~ tr|+/=|-_|dr }
  42         75  
  42         62  
  42         503  
8 37     37 0 80827 sub md5_ushort ($str, $len) { substr(md5_ubase64($str),0,$len) }
  37         89  
  37         93  
  37         53  
  37         99  
9              
10             # Sleep for a fractional number of seconds
11 18     18 1 799263 sub minisleep ($seconds) { select(undef, undef, undef, $seconds) }
  18         97  
  18         57  
  18         5018698  
12              
13             # Module util (inspired by Module::Loaded)
14 347     347 0 498 sub name_to_pm ($name) { $name =~ s|::|/|gr . '.pm' }
  347         513  
  347         471  
  347         204611  
15 76     76 0 160 sub mark_module_loaded ($name) { $INC{name_to_pm($name)} = 'DUMMY' }
  76         191  
  76         108  
  76         195  
16 268     268 0 19156 sub is_module_loaded ($name) { exists $INC{name_to_pm($name)} }
  268         470  
  268         382  
  268         659  
17 2     2 0 6 sub is_module_ok ($name) { defined $INC{name_to_pm($name)} }
  2         4  
  2         3  
  2         6  
18 114 100   114 0 229 sub is_module_broken ($name) { is_module_loaded($name) and !is_module_ok($name) }
  114         206  
  114         157  
  114         252  
19              
20             # JSON and Base 64
21 7     7 0 11 sub json_codec () {
  7         10  
22 7         1259 require JSON::MaybeXS;
23 7         27636 state $json_codec = JSON::MaybeXS->new(utf8 => 1);
24 7         233 $json_codec;
25             }
26              
27 4     4 0 8 sub encode_json ($dat) { json_codec->encode($dat) }
  4         11  
  4         9  
  4         9  
28 3     3 0 8 sub decode_json ($str) { json_codec->decode($str) }
  3         5  
  3         6  
  3         9  
29 4     4 0 999 sub encode_ju64 ($dat) { b64_to_u64(MIME::Base64::encode(encode_json($dat))) }
  4         7  
  4         7  
  4         20  
30 3     3 0 686 sub decode_ju64 ($str) { decode_json(MIME::Base64::decode(u64_to_b64($str))) }
  3         8  
  3         4  
  3         14  
31 5     5 0 4915 sub b64_to_u64 ($str) { $str =~ tr`+/=\n`-_`dr }
  5         11  
  5         7  
  5         70  
32 4     4 0 8 sub u64_to_b64 ($str) { $str =~ tr`-_`+/`r }
  4         9  
  4         6  
  4         37  
33             }
34              
35             1;
36              
37             =pod
38              
39             =head1 NAME
40              
41             PXF::Util - Utilities for PlackX::Framework
42              
43              
44             =head1 SYNOPSIS
45              
46             use PXF::Util;
47             PXF::Uil::md5_ubase64($string);
48             ...
49              
50              
51             =head1 EXPORTS
52              
53             None.
54              
55              
56             =head1 FUNCTIONS
57              
58             =over 4
59              
60             =item md5_urlbase64($string)
61              
62             Returns the md5 of $string in base64, replacing url-unsafe characters with
63             safe ones.
64              
65             =item md5_urlshort($string, $len)
66              
67             Returns a shortened url-safe base64 md5 of $string.
68              
69             =item minisleep(seconds)
70              
71             Sleep for a fractional number of seconds.
72              
73             =back
74              
75             =head1 META
76              
77             For copyright and license, see PlackX::Framework.