blib/lib/Chicken/Ipsum.pm

filter: Statementstmt 100% 66 / 66 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 100% 103 / 103 CC 11 SCAR 8.5
CC
11
Cov
100%
new
4.0
words
4.0
sentences
3.0
CRAP 2.3
↑ Summary chicken-ipsum.in »
1 package Chicken::Ipsum 1.000002;
2 14 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Chicken::Ipsum - Generate random chicken noises
8  
9 =head1 SYNOPSIS
10  
11 require Chicken::Ipsum;
12  
13 my $ci = Chicken::Ipsum->new();
14  
15 # Generate a string of text with 5 words
16 $words = $ci->words(5);
17  
18 # Generate a list of 5 words
19 @words = $ci->words(5);
20  
21 # Generate a string of text with 2 sentences
22 $sentences = $ci->sentences(2);
23  
24 # Generate a list of 2 sentences
25 @sentences = $ci->sentences(2);
26  
27 # Generate a string of text with 3 paragraphs
28 $paragraphs = $ci->paragraphs(3);
29  
30 # Generate a list of 3 paragraphs
31 @paragraphs = $ci->paragraphs(3);
32  
33 =head1 DESCRIPTION
34  
35 Often when developing a website or other application, it's important to have
36 placeholders for content. This module generates prescribed amounts of clucking,
37 cawing and other chicken-y noises.
38  
39 =cut
40  
41 14 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 14 ▶use List::Util 1.54 qw/ sample /;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 14 ▶use constant WORDS => [qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
45 puk
46 pukaaak
47 cluck
48 cluck-cluck-cluck
49 cluckity
50 bwak
51 waaak
52 bok
53 bwok
54 cluck-a-buh-gawk
55 cock-a-doodle-doo
56 bwwwaaaaaaaaaak
57 gobble-gobble
58 honk
59 14 /];
60 14 ▶use constant PUNCTUATIONS => [qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
61 .
62 ...
63 !
64 ?
65 14 /];
66 14 ▶use constant MIN_SENTENCE_WORDS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 14 ▶use constant MAX_SENTENCE_WORDS => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 14 ▶use constant MIN_PARAGRAPH_SENTENCES => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 14 ▶use constant MAX_PARAGRAPH_SENTENCES => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 =head1 CONSTRUCTOR
72  
73 =head2 C<new()>
74  
75 my $ci = Chicken::Ipsum->new( %options )
76  
77 This method constructs a new L<Chicken::Ipsum> object and returns it. Key/value
78 pair arguments may be provided to set up the initial state. The following
79 options are recognized:
80  
81 KEY DEFAULT
82 ----------- --------------------
83 frantic 0.1
84  
85 =over
86  
87 =item frantic
88  
89 Randomly capitalize words with the given ratio.
90  
91 =back
92  
93 =cut
94  
95 12 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
96 12 my ($class, %args) = @_;
97 12 my $self = bless {
98 frantic => 0.1,
99 }, $class;
100  
101 12 foreach my $opt (keys %{$self}) {
102 12 ▶ if (exists $args{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$opt}) 3 9 12
103 3 $self->{$opt} = delete $args{$opt};
104 }
105 }
106 # Ensure all incoming arguments were used
107 12 ▶ if (%args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) 1 11 12
108 1 croak('Unrecognized argument(s): ', join ', ', sort keys %args);
109 }
110 11 return $self;
111 }
112  
113 =head1 METHODS
114  
115 All methods below will return a string in scalar context or a list in list
116 context.
117  
118 =head2 C<words( INTEGER )>
119  
120 Returns INTEGER Chicken words.
121  
122 =cut
123  
124 740 ▶sub words {
Subroutine words: called CC=4 SCAR=13.9
Pod documented
125 740 my ($self, $num) = @_;
126 740 my @words = sample $num, @{+WORDS};
127 740 foreach my $word (@words) {
128 4816 ▶ if (rand 1 < $self->{frantic}) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand 1 < $self->{"frantic"}) 811 4005 4816
129 811 $word = uc $word;
130 }
131 }
132 740 ▶ return wantarray ? @words : "@words";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 739 740
133 }
134  
135 =head2 C<sentences( INTEGER )>
136  
137 Returns INTEGER sentences in Chicken.
138  
139 =cut
140  
141 143 ▶sub sentences {
Subroutine sentences: called CC=3 SCAR=11.0
Pod documented
142 143 my ($self, $num) = @_;
143 143 my @sentences;
144 # Sentences remaining "goes to" 0, LOL.
145 # (See https://stackoverflow.com/q/1642028/237955)
146 143 ▶ while ($num --> 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num-- > 0) 733 143 876
147 733 push @sentences, $self->_get_sentence();
148 }
149 143 ▶ return wantarray ? @sentences : "@sentences";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 141 143
150 }
151  
152 =head2 C<paragraphs( INTEGER )>
153  
154 Returns INTEGER paragraphs of Chicken text.
155  
156 =cut
157  
158 11 ▶sub paragraphs {
Subroutine paragraphs: called CC=3 SCAR=11.0
Pod documented
159 11 my ($self, $num) = @_;
160 11 my @paragraphs;
161 11 ▶ while ($num --> 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num-- > 0) 135 11 146
162 135 push @paragraphs, $self->_get_paragraph;
163 }
164 11 ▶ return wantarray ? @paragraphs : join "\n\n", @paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 9 11
165 }
166  
167 135 ▶sub _get_paragraph {
Subroutine _get_paragraph: called CC=1 SCAR=0.0
168 135 my $self = shift;
169 135 my $num = MIN_PARAGRAPH_SENTENCES + int rand MAX_PARAGRAPH_SENTENCES - MIN_PARAGRAPH_SENTENCES;
170 135 my $paragraph = $self->sentences($num);
171 135 return $paragraph;
172 }
173  
174 733 ▶sub _get_punctuation {
Subroutine _get_punctuation: called CC=1 SCAR=0.0
175 733 return sample 1, @{+PUNCTUATIONS};
176 }
177  
178 733 ▶sub _get_sentence {
Subroutine _get_sentence: called CC=1 SCAR=0.0
179 733 my $self = shift;
180 733 my $num = MIN_SENTENCE_WORDS + int rand MAX_SENTENCE_WORDS - MIN_SENTENCE_WORDS;
181 733 my $words = ucfirst $self->words($num);
182 733 return $words . _get_punctuation();
183 }
184  
185 =head1 AUTHOR
186  
187 Dan Church (h3xx<attyzatzat>gmx<dottydot>com)
188  
189 =head1 SEE ALSO
190  
191 L<Text::Lorem>
192  
193 L<https://isotropic.org/papers/chicken.pdf>
194  
195 =head1 LICENSE AND COPYRIGHT
196  
197 Copyright (C) 2023 Dan Church.
198  
199 This library is free software; you can redistribute it and/or modify it under
200 the same terms as Perl itself.
201  
202 =head1 AVAILABILITY
203  
204 The latest version of this library is likely to be available from CPAN as well
205 as:
206  
207 L<https://codeberg.org/h3xx/perl-Chicken-Ipsum>
208  
209 =head1 THANKS
210  
211 Thanks to Sebastian Carlos's L<https://chickenipsum.lol/>
212 (L<https://github.com/sebastiancarlos/chicken-ipsum>) for the inspiration.
213  
214 =cut
215 14 1;
↑ Summary chicken-ipsum.in »