blib/lib/Docker/Names/Random.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 93.6% 74 / 79 CC 4 SCAR 5.9
CC
4
Cov
95%
docker_name
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.8
↑ Summary
1 package Docker::Names::Random;
2  
3 ## no critic (Documentation::RequirePodAtEnd)
4 ## no critic (Documentation::RequirePodSections)
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use feature qw( state );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use utf8; # The __DATA__ is UTF-8
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Create random strings like Docker does for container names.
14  
15 3 our $VERSION = '0.0.2'; # VERSION: generated by DZP::OurPkgVersion
16  
17 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 our @EXPORT_OK = qw( docker_name );
19 3 our %EXPORT_TAGS = ( 'all' => [qw( docker_name )] );
20  
21 3 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Hash::Util qw( lock_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 # Internal functions
26  
27 2002 ▶sub _get_data {
Subroutine _get_data: called CC=1 SCAR=0.0
28 2002 state $ypp = YAML::PP->new;
29 2002 local $INPUT_RECORD_SEPARATOR = undef;
30 2002 state $data = $ypp->load_string(<DATA>);
31 2002 return $data;
32 }
33  
34 1002 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
35 1002 my $class = shift;
36  
37 1002 my $data = _get_data();
38 1002 my $self = { data => $data };
39 1002 bless $self, $class;
40 1002 lock_keys( %{$self} );
41 1002 return $self;
42 }
43  
44  
45 3000 ▶sub docker_name {
Subroutine docker_name: called CC=4 SCAR=14.0
Pod documented
46 3000 my $self = shift;
47 3000 my $data;
48 3000 ▶ if ( defined $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) { } 2000 1000 3000
49 2000 $data = $self->{'data'};
50 }
51 else {
52  
53 # We are not running as an object method
54 1000 $data = _get_data();
55 }
56  
57 3000 my ( $adj, $surname );
58  
59 CREATE_NAME:
60 3000 my $r_for_adj = int( rand( scalar @{ $data->{'adjectives'} } ) );
61 3000 $adj = $data->{'adjectives'}->[$r_for_adj]->{'word'};
62 3000 my $r_for_name = int( rand( scalar @{ $data->{'names'} } ) );
63 3000 $surname = $data->{'names'}->[$r_for_name]->{'surname'};
64  
65 3000 ▶ if ( $adj eq 'boring' && $surname eq 'wozniak' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adj eq "boring" and $surname eq "wozniak") 0 3000 3000
Condition66%2/3
$adj eq "boring" and $surname eq "wozniak"
!ll&&!rl&&r
2974260
MC/DC0%0/2
$adj eq "boring" and $surname eq "wozniak"
$adj eq "boring" $surname eq "wozniak"
Truth table66%2/3
A and B
A= $adj eq "boring"B= $surname eq "wozniak"
ABresult
0X0
100
111
66  
67 # Steve Wozniak is not boring
68 0 goto 'CREATE_NAME';
69 }
70  
71 3000 return $adj . q{_} . $surname;
72 }
73  
74  
75 3 1;
76  
77 =pod
78  
79 =encoding UTF-8
80  
81 =head1 NAME
82  
83 Docker::Names::Random - Create random strings like Docker does for container names.
84  
85 =head1 VERSION
86  
87 version 0.0.2
88  
89 =head1 SYNOPSIS
90  
91 # As an object (if you create many, this is more efficient).
92 require Docker::Names::Random;
93  
94 my $dnr = Docker::Names::Random->new();
95 my $random_name1 = $dnr->docker_name();
96  
97 # As an imported function.
98 use Docker::Names::Random qw( docker_name );
99 # OR
100 use Docker::Names::Random qw( :all );
101  
102 my $random_name2 = docker_name();
103  
104 =head1 DESCRIPTION
105  
106 If you are using Docker, you may have noticed that it
107 creates random names for containers when you haven't provided any
108 specific name. These names are a combination of an adjective
109 and a proper name of an individual. The individuals are
110 famous men and women picked from the history of scientific exploration
111 and engineering.
112  
113 This package allows you to use the same system in your own programs.
114 You would get combinations like
115 I<interesting_mendeleev>,
116 I<epic_engelbart>,
117 I<lucid_dhawan>,
118 I<recursing_cori>,
119 I<ecstatic_liskov> and
120 I<busy_ardinghelli>.
121  
122 The combination I<boring_wozniak> is not allowed because
123 L<Steve Wozniak|https://en.wikipedia.org/wiki/Steve_Wozniak> is not boring.
124 This same limitation exists in the
125 L<original code|https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go>.
126  
127 =for stopwords overthruster Docker::Names::Random Docker Names Random
128  
129 =head1 STATUS
130  
131 This module is currently being developed so changes in the API are possible,
132 though unlikely.
133  
134 =head1 METHODS
135  
136 =head2 new
137  
138 Create object.
139  
140 require Docker::Names::Random;
141 my $dnr = Docker::Names::Random->new();
142  
143 =head2 docker_name
144  
145 Return a random string.
146  
147 my $random_name = $dnr->docker_name();
148  
149 =head1 FUNCTIONS
150  
151 =head2 docker_name
152  
153 Return a random string.
154  
155 use Docker::Names::Random qw( docker_name );
156 # OR
157 use Docker::Names::Random qw( :all );
158  
159 my $random_name = docker_name();
160  
161 =head1 AUTHOR
162  
163 Mikko Johannes Koivunalho <mikko.koivunalho@iki.fi>
164  
165 =head1 COPYRIGHT AND LICENSE
166  
167 This software is copyright (c) 2020 by Mikko Johannes Koivunalho.
168  
169 This is free software; you can redistribute it and/or modify it under
170 the same terms as the Perl 5 programming language system itself.
171  
172 =cut
173  
174 __DATA__
↑ Summary