| 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
| |||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | 0 | ▶ | die 'failed to parse size: '.$size | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 52 | 0 | ▶ | $unit eq 'G' ? $size *= 1024*1024*1024 | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 75 | 0 | $size = int($size/1024); | |||||||||||||||||
| 76 | 0 | $unit = 'K'; | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | 0 | ▶ | if ($size > 1024*2) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | 0 | $size = int($size/1024); | |||||||||||||||||
| 80 | 0 | $unit = 'M'; | |||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | 0 | ▶ | if ($size > 1024*2) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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__ | ||||||||||||||||||