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   288288 use v5.36;
  12         47  
2             package PXF::Util {
3 12     12   4419 use MIME::Base64 ();
  12         8016  
  12         325  
4 12     12   89 use Digest::MD5 ();
  12         16  
  12         4821  
5              
6             # MD5 digests, url-encoded
7 42     42 0 2615 sub md5_ubase64 ($str) { Digest::MD5::md5_base64($str) =~ tr|+/=|-_|dr }
  42         44  
  42         35  
  42         317  
8 37     37 0 16665 sub md5_ushort ($str, $len) { substr(md5_ubase64($str),0,$len) }
  37         52  
  37         38  
  37         37  
  37         58  
9              
10             # Sleep for a fractional number of seconds
11 18     18 1 783693 sub minisleep ($seconds) { select(undef, undef, undef, $seconds) }
  18         65  
  18         53  
  18         5006601  
12              
13             # Module util (inspired by Module::Loaded)
14 347     347 0 360 sub name_to_pm ($name) { $name =~ s|::|/|gr . '.pm' }
  347         370  
  347         347  
  347         161611  
15 76     76 0 84 sub mark_module_loaded ($name) { $INC{name_to_pm($name)} = 'DUMMY' }
  76         95  
  76         104  
  76         155  
16 268     268 0 8467 sub is_module_loaded ($name) { exists $INC{name_to_pm($name)} }
  268         288  
  268         259  
  268         418  
17 2     2 0 3 sub is_module_ok ($name) { defined $INC{name_to_pm($name)} }
  2         2  
  2         2  
  2         3  
18 114 100   114 0 175 sub is_module_broken ($name) { is_module_loaded($name) and !is_module_ok($name) }
  114         159  
  114         111  
  114         178  
19              
20             # JSON and Base 64
21 7     7 0 8 sub json_codec () {
  7         8  
22 7         937 require JSON::MaybeXS;
23 7         13507 state $json_codec = JSON::MaybeXS->new(utf8 => 1);
24 7         207 $json_codec;
25             }
26              
27 4     4 0 4 sub encode_json ($dat) { json_codec->encode($dat) }
  4         5  
  4         4  
  4         11  
28 3     3 0 4 sub decode_json ($str) { json_codec->decode($str) }
  3         5  
  3         5  
  3         6  
29 4     4 0 656 sub encode_ju64 ($dat) { b64_to_u64(MIME::Base64::encode(encode_json($dat))) }
  4         7  
  4         6  
  4         15  
30 3     3 0 412 sub decode_ju64 ($str) { decode_json(MIME::Base64::decode(u64_to_b64($str))) }
  3         6  
  3         4  
  3         7  
31 5     5 0 778 sub b64_to_u64 ($str) { $str =~ tr`+/=\n`-_`dr }
  5         7  
  5         6  
  5         36  
32 4     4 0 4 sub u64_to_b64 ($str) { $str =~ tr`-_`+/`r }
  4         6  
  4         15  
  4         42  
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.