Test/Server/Util.pm

filter: Statementstmt 37.5% 12 / 32 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 3 / 5 Podpod 100% 2 / 2 totaltotal 29.8% 17 / 57 CC 10 SCAR 35.2
CC
10
Cov
24%
parse_size
56.0
format_size
20.0
BEGIN
1.0
CRAP 33.9
« Sys/Net.pm ↑ Summary Test/Server.pm »
1 package # hide from CPAN indexer
2 Test::Server::Util;
3  
4 =head1 NAME
5  
6 Test::Server::Util - some usefull functions for Test::Server files
7  
8 =head1 SYNOPSIS
9  
10 use Test::Server::Util qw(parse_size format_size);
11
12 print parse_size('5G'), "\n";
13 print format_size(5*1024*1024*1024), "\n";
14  
15 =head1 DESCRIPTION
16  
17 Some usefull functions for Test::Server files. See FUNCTION section.
18  
19 =cut
20  
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '0.06';
25  
26 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 our @EXPORT_OK = qw(format_size parse_size);
28  
29  
30 =head1 FUNCTIONS
31  
32 =head2 parse_size($size)
33  
34 $size can be a number with optional sufix one of [GMK]. Returns
35 size in bytes.
36  
37 =cut
38  
39 0 ▶sub parse_size {
Subroutine parse_size: not called CC=7 SCAR=40.3
Pod documented
40 0 my $size = shift;
41
42 return
43 0 ▶ if not defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 0 0
44
45 0 ▶ die 'failed to parse size: '.$size
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /\b([0-9]+)\s*([MKG]?)\s*$/ 0 0 0
46 if ($size !~ m/\b([0-9]+)\s*([MKG]?)\s*$/);
47
48 0 $size = $1;
49 0 my $unit = $2;
50
51 0 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 0 0 0
52 0 ▶ $unit eq 'G' ? $size *= 1024*1024*1024
Branch: 3 branches
BranchTrueFalseTotal
$unit eq 'G' ? : 0 0 0
$unit eq 'M' ? : 0 0 0
$unit eq 'K' ? : 0 0 0
53 : $unit eq 'M' ? $size *= 1024*1024
54 : $unit eq 'K' ? $size *= 1024
55 : die 'shoud never happend... but if enjoy! ;)';
56 }
57
58 0 return $size;
59 }
60  
61  
62 =head2 format_size($size)
63  
64 $size should be number of bytes. Returns number of kilo/mega/giga
65 bytes value formated with sufix (one of [KMG]).
66  
67 =cut
68  
69 0 ▶sub format_size {
Subroutine format_size: not called CC=4 SCAR=30.0
Pod documented
70 0 my $size = shift;
71
72 0 my $unit = '';
73
74 0 ▶ if ($size > 1024*2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 2048) 0 0 0
75 0 $size = int($size/1024);
76 0 $unit = 'K';
77 }
78 0 ▶ if ($size > 1024*2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 2048) 0 0 0
79 0 $size = int($size/1024);
80 0 $unit = 'M';
81 }
82 0 ▶ if ($size > 1024*2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 2048) 0 0 0
83 0 $size = int($size/1024);
84 0 $unit = 'G';
85 }
86
87 0 return $size.$unit;
88 }
89  
90 1 'V nasej obci - Jaro Filip';
91  
92  
93 __END__
« Sys/Net.pm ↑ Summary Test/Server.pm »