File Coverage

blib/lib/Chicken/Ipsum.pm
Criterion Covered Total %
statement 65 65 100.0
branch 12 12 100.0
condition n/a
subroutine 17 17 100.0
pod 4 4 100.0
total 98 98 100.0


line stmt bran cond sub pod time code
1             package Chicken::Ipsum 1.000002;
2 14     14   2909735 use 5.012;
  14         71  
3 14     14   108 use warnings;
  14         39  
  14         1281  
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     14   777 use Carp qw/ croak /;
  14         39  
  14         1406  
42 14     14   114 use List::Util 1.54 qw/ sample /;
  14         356  
  14         2150  
43              
44 14         1702 use constant WORDS => [qw/
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     14   108 /];
  14         52  
60 14         25539 use constant PUNCTUATIONS => [qw/
61             .
62             ...
63             !
64             ?
65 14     14   145 /];
  14         27  
66 14     14   92 use constant MIN_SENTENCE_WORDS => 4;
  14         30  
  14         812  
67 14     14   236 use constant MAX_SENTENCE_WORDS => 10;
  14         49  
  14         821  
68 14     14   130 use constant MIN_PARAGRAPH_SENTENCES => 3;
  14         29  
  14         918  
69 14     14   88 use constant MAX_PARAGRAPH_SENTENCES => 7;
  14         26  
  14         12290  
70              
71             =head1 CONSTRUCTOR
72              
73             =head2 C
74              
75             my $ci = Chicken::Ipsum->new( %options )
76              
77             This method constructs a new L 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             sub new {
96 12     12 1 542661 my ($class, %args) = @_;
97 12         57 my $self = bless {
98             frantic => 0.1,
99             }, $class;
100              
101 12         35 foreach my $opt (keys %{$self}) {
  12         99  
102 12 100       64 if (exists $args{$opt}) {
103 3         10 $self->{$opt} = delete $args{$opt};
104             }
105             }
106             # Ensure all incoming arguments were used
107 12 100       49 if (%args) {
108 1         238 croak('Unrecognized argument(s): ', join ', ', sort keys %args);
109             }
110 11         55 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
119              
120             Returns INTEGER Chicken words.
121              
122             =cut
123              
124             sub words {
125 762     762 1 31417 my ($self, $num) = @_;
126 762         1150 my @words = sample $num, @{+WORDS};
  762         3250  
127 762         1556 foreach my $word (@words) {
128 4829 100       10787 if (rand 1 < $self->{frantic}) {
129 743         1619 $word = uc $word;
130             }
131             }
132 762 100       3395 return wantarray ? @words : "@words";
133             }
134              
135             =head2 C
136              
137             Returns INTEGER sentences in Chicken.
138              
139             =cut
140              
141             sub sentences {
142 143     143 1 273 my ($self, $num) = @_;
143 143         223 my @sentences;
144             # Sentences remaining "goes to" 0, LOL.
145             # (See https://stackoverflow.com/q/1642028/237955)
146 143         325 while ($num --> 0) {
147 755         1593 push @sentences, $self->_get_sentence();
148             }
149 143 100       956 return wantarray ? @sentences : "@sentences";
150             }
151              
152             =head2 C
153              
154             Returns INTEGER paragraphs of Chicken text.
155              
156             =cut
157              
158             sub paragraphs {
159 11     11 1 36 my ($self, $num) = @_;
160 11         22 my @paragraphs;
161 11         41 while ($num --> 0) {
162 135         345 push @paragraphs, $self->_get_paragraph;
163             }
164 11 100       149 return wantarray ? @paragraphs : join "\n\n", @paragraphs;
165             }
166              
167             sub _get_paragraph {
168 135     135   258 my $self = shift;
169 135         535 my $num = MIN_PARAGRAPH_SENTENCES + int rand MAX_PARAGRAPH_SENTENCES - MIN_PARAGRAPH_SENTENCES;
170 135         325 my $paragraph = $self->sentences($num);
171 135         437 return $paragraph;
172             }
173              
174             sub _get_punctuation {
175 755     755   1091 return sample 1, @{+PUNCTUATIONS};
  755         4175  
176             }
177              
178             sub _get_sentence {
179 755     755   1290 my $self = shift;
180 755         1508 my $num = MIN_SENTENCE_WORDS + int rand MAX_SENTENCE_WORDS - MIN_SENTENCE_WORDS;
181 755         1645 my $words = ucfirst $self->words($num);
182 755         1461 return $words . _get_punctuation();
183             }
184              
185             =head1 AUTHOR
186              
187             Dan Church (h3xxgmxcom)
188              
189             =head1 SEE ALSO
190              
191             L
192              
193             L
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
208              
209             =head1 THANKS
210              
211             Thanks to Sebastian Carlos's L
212             (L) for the inspiration.
213              
214             =cut
215             1;