File Coverage

blib/lib/Number/DataRate.pm
Criterion Covered Total %
statement 30 30 100.0
branch 15 20 75.0
condition 5 5 100.0
subroutine 6 6 100.0
pod 0 2 0.0
total 56 63 88.8


line stmt bran cond sub pod time code
1             package Number::DataRate;
2 1     1   22017 use strict;
  1         2  
  1         32  
3 1     1   6 use warnings;
  1         2  
  1         23  
4 1     1   810 use Regexp::Common;
  1         4847  
  1         8  
5 1     1   70265 use base qw(Class::Accessor::Faster);
  1         3  
  1         892  
6             our $VERSION = '0.31';
7              
8             sub to_bytes_per_second {
9 24     24 0 46 my ( $self, $value ) = @_;
10 24         44 return $self->to_bits_per_second($value) / 8;
11             }
12              
13             sub to_bits_per_second {
14 48     48 0 109 my ( $self, $string ) = @_;
15              
16             #warn $value;
17 48         229 $string =~ m{^
18             ($RE{num}{real}{-sep => ',?'}) # value
19             \s* # whitespace
20             ([kMGT]i?)? # magnitude
21             (bit|b|byte|B|Byte) # unit
22             (/sec|/s|ps|psec) # /sec
23             $}x;
24 48         7398 my ( $value, $magnitude, $unit, $psec ) = ( $1, $2, $3, $4 );
25 48 50       108 return unless $psec;
26 48         65 $value =~ s/,//g;
27 48   100     99 $magnitude ||= '';
28              
29             #warn "$1 / $2 / $3 / $4";
30              
31 48 100 100     209 $value *= 8 if $unit eq 'B' || lc($unit) eq 'byte';
32              
33 48 100       102 $value *= 1_000 if $magnitude eq 'k';
34 48 50       76 $value *= 1024 if $magnitude eq 'ki';
35 48 100       78 $value *= 1_000_000 if $magnitude eq 'M';
36 48 50       69 $value *= 1024 * 1024 if $magnitude eq 'Mi';
37 48 100       96 $value *= 1_000_000_000 if $magnitude eq 'G';
38 48 50       66 $value *= 1024 * 1024 * 1024 if $magnitude eq 'Gi';
39 48 100       75 $value *= 1_000_000_000_000 if $magnitude eq 'T';
40 48 50       68 $value *= 1024 * 1024 * 1024 * 1024 if $magnitude eq 'Ti';
41 48         303 return $value;
42             }
43              
44             1;
45              
46             __END__
47              
48             =head1 NAME
49              
50             Number::DataRate - Convert data rate to bits or bytes per second
51              
52             =head1 SYNOPSIS
53              
54             # Bluetooth 2.0+EDR has a data rate of 3000 kbit/s, but what is that
55             # in bits and bytes per second?
56             use Number::DataRate;
57             my $data_rate = Number::DataRate->new;
58             my $bits_per_second = $data_rate->to_bits_per_second('3000 kbit/s');
59             my $bytes_per_second = $data_rate->to_bytes_per_second('3000 kbit/s');
60              
61             =head1 DESCRIPTION
62              
63             This module convers data rate to bits or bytes per second. For far
64             more than you ever wanted to know about data rates, see Wikipedia:
65              
66             http://en.wikipedia.org/wiki/Data_rate_units
67              
68             This module accepts a wide range of formats, for example:
69              
70             0.045 kbit/s
71             0.045 kbps
72             5.625Byte/s
73             24,576 kbit/s
74             54.0 Mbit/s
75             6.4 Gbit/s
76             1.28Tbit/s
77              
78             =head1 AUTHOR
79              
80             Leon Brocard <acme@astray.com>.
81              
82             =head1 COPYRIGHT
83              
84             Copyright (C) 2008, Leon Brocard
85              
86             This module is free software; you can redistribute it or modify it
87             under the same terms as Perl itself.