| 1 | package Number::Stars; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 4 | ▶ | use Class::Utils qw(set_params); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 4 | ▶ | use Error::Pure qw(err); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 4 | our $VERSION = 0.01; | |||||||||||||
| 10 | |||||||||||||||
| 11 | # Constructor. | ||||||||||||||
| 12 | sub new { | ||||||||||||||
| 13 | 6 | ▶ | my ($class, @params) = @_; | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | # Create object. | ||||||||||||||
| 16 | 6 | my $self = bless {}, $class; | |||||||||||||
| 17 | |||||||||||||||
| 18 | # Number of stars. | ||||||||||||||
| 19 | 6 | $self->{'number_of_stars'} = 10; | |||||||||||||
| 20 | |||||||||||||||
| 21 | # Process parameters. | ||||||||||||||
| 22 | 6 | set_params($self, @params); | |||||||||||||
| 23 | |||||||||||||||
| 24 | 5 | ▶ | if ($self->{'number_of_stars'} !~ m/^\d+$/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 1 | err "Parameter 'number_of_stars' must be a number."; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 4 | return $self; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | # Convert percent number to stars definition. | ||||||||||||||
| 32 | sub percent_stars { | ||||||||||||||
| 33 | 6 | ▶ | my ($self, $percent) = @_; | ||||||||||||
|
Subroutine
percent_stars: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 6 | my $stars_hr = {}; | |||||||||||||
| 36 | 6 | my $star_percent = 100 / $self->{'number_of_stars'}; | |||||||||||||
| 37 | 6 | foreach my $star_id (1 .. $self->{'number_of_stars'}) { | |||||||||||||
| 38 | 53 | ▶ | if ($percent >= $star_id * $star_percent) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 39 | 26 | $stars_hr->{$star_id} = 'full'; | |||||||||||||
| 40 | } elsif ($percent >= ($star_id * $star_percent) - ($star_percent / 2)) { | ||||||||||||||
| 41 | 2 | $stars_hr->{$star_id} = 'half', | |||||||||||||
| 42 | } else { | ||||||||||||||
| 43 | 25 | $stars_hr->{$star_id} = 'nothing', | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 6 | return $stars_hr; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | __END__ | ||||||||||||||