More.pm untested

filter: Statementstmt 0.0% 0 / 190 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 307 CC 32 SCAR 39.6
CC
32
Cov
0%
_process
110.0
_generate
90.0
_replace_pattern
72.0
CRAP 52.7
1 package Text::Lorem::More;
2  
3 use warnings;
4 use strict;
5  
6 =head1 NAME
7  
8 Text::Lorem::More - Generate formatted nonsense using random Latin words.
9  
10 =head1 VERSION
11  
12 Version 0.13
13  
14 =head1 SYNOPSIS
15  
16 Generate formatted nonsense using random Latin words.
17  
18 use Text::Lorem::More;
19  
20 my $lorem = Text::Lorem::More->new;
21
22 # Greet a friend
23 print "Hello, ", $lorem->fullname, "\n";
24  
25 # You could also ...
26 print $lorem->process("Hello, +fullname\n");
27  
28 ... or you can use the singleton:
29  
30 use Text::Lorem::More qw(lorem);
31  
32 # Generate a random latin word
33 my $latinwordoftheday = lorem->word;
34  
35 # Produce paragaphs in the Text::Lorem compatible manner
36 my $content = lorem->paragraphs(3);
37  
38 # Print 4 paragraphs, each separated by a single newline and tab:
39 print "\t", scalar lorem->paragraph(4, "\n\t");
40  
41 =cut
42  
43 our $VERSION = '0.13';
44  
45 use base qw/Exporter/;
46  
47 use Carp;
48 use Parse::RecDescent;
49  
50 use Text::Lorem::More::Source;
51
52 our $PARSER = Parse::RecDescent->new(<<'_END_');
53 content: <rulevar: local @content >
54 content: <skip:''> pattern(s) { \@content }
55 pattern: escape | variable | text
56 escape: '++' { push @content, \"+" }
57 variable: '+{' identifier '}' | '+' identifier
58 identifier: /[A-Za-z0-9_]+/ { push @content, $item[1] }
59 text: m/[^\+]+/ { push @content, \$item[1] }
60 _END_
61  
62 =head1 GENERATORS
63  
64 To use a generator, call the method with the same name as the generator,
65 To use the name generator, for example:
66  
67 my $name = $lorem->name;
68  
69 Alternatively, you can use the C<generate> or C<process> functions.
70 To use the hostname generator, for example:
71  
72 my $hostname = $lorem->hostname;
73  
74 # This will do the same thing ...
75 my $otherhostname = $lorem->generate("+hostname");
76  
77 =head2 word
78  
79 Generates random latin word.
80  
81 dicta
82 sed
83 repellat
84  
85 =head2 sentence
86  
87 Generates between 4 and 9 words, with the first letter of the first word capitalized and a period following
88 the last word.
89  
90 =head2 paragraph
91  
92 Generates between 3 and 6 sentences,
93  
94 =head2 words $count
95  
96 A Text::Lorem compatible words generator.
97 Will generate $count words joined by " ".
98  
99 =head2 sentences $count
100  
101 A Text::Lorem compatible sentences generator.
102 Will generate $count sentences joined by " ".
103 Each sentence contains between 4 and 9 words and ends with a period.
104 B<Passing in 0 for count will still produce a single period!>
105  
106 =head2 paragraphs $count
107  
108 A Text::Lorem compatible sentences generator.
109 Will generate $count paragraphs joined by "\n\n".
110 Each paragraph contains between 3 and 6 sentences.
111  
112 =head2 name / firstname / lastname
113  
114 Generates a random latin word with the first letter capitalized
115  
116 Repellat
117 Sed
118 Ipsum
119  
120 =head2 fullname
121  
122 Generates a firstname and lastname separated by a space
123  
124 Lorem Dicta
125  
126 =head2 username
127  
128 Generates a random latin word
129  
130 =head2 title
131  
132 Generates between 1 and 3 words with the first letter of the first word capitalized
133  
134 =head2 description
135  
136 Generates between 1 and 3 sentences.
137  
138 =head2 tld / topleveldomain
139  
140 Generates a top level domain.
141 Currently, this will either be "com", "org", or "net".
142  
143 =head2 domain / domainname
144  
145 Generates a domainname.
146 Currently, this will attach "example" to a tld generator result.
147  
148 example.com
149 example.net
150  
151 =head2 host / hostname
152  
153 Generates a hostname.
154 Currently, this will either return a plain domainname, as above, or attach a latin word to a domainname result.
155  
156 et.example.com
157 example.org
158  
159 =head2 email / mail
160  
161 =head2 path
162  
163 =head2 httpurl
164  
165 =head2 mailto
166  
167 =cut
168  
169 our %GENERATOR = (
170  
171 name => sub { [ sub { ucfirst lc $_ }, "+word" ] },
172 firstname => "name",
173 lastname => "name",
174  
175 fullname => sub { ["+firstname +lastname"] },
176  
177 username => "word",
178  
179 word => [ grep { length $_ } map { s/\W//g; lc } split m/\s/, <<_END_ ],
180 alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis Nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium, totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, Sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est, qui minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores doloribus asperiores repellat.
181 _END_
182  
183 title => sub { [ sub { ucfirst($_) }, "+word", 1 + int rand 3 ] },
184  
185 description => sub { [ "+sentence", 1 + int rand 3 ] },
186  
187 sentence => sub { [ sub { ucfirst($_) . "." }, "+word", 4 + int rand 6 ] },
188  
189 paragraph => sub { [ "+sentence", 3 + int rand 4 ] },
190  
191 words => sub {
192 $Text::Lorem::More::PRUNE = 1;
193 return [ "+word", $Text::Lorem::More::COUNT, " " ];
194 },
195  
196 sentences => sub {
197 my $lorem = shift;
198 $Text::Lorem::More::PRUNE = 1;
199 my $count = $Text::Lorem::More::COUNT;
200 my @sentence;
201 while ($count > 0) {
202 push @sentence, ucfirst $lorem->words(4 + int rand 6);
203 $count--;
204 }
205 return join(". ", @sentence) . ".";
206 },
207  
208 paragraphs => sub {
209 my $lorem = shift;
210 $Text::Lorem::More::PRUNE = 1;
211 my $count = $Text::Lorem::More::COUNT;
212 my @paragraph;
213 while ($count > 0) {
214 push @paragraph, $lorem->sentences(3 + int rand 4);
215 $count--;
216 }
217 return join("\n\n", @paragraph);
218 },
219  
220 email => [ map { [ $_ ] } split m/\n/, <<_END_ ],
221 +word\@+hostname
222 +word\@+domainname
223 _END_
224  
225 mail => "email",
226  
227 relativepath => '',
228 absolutepath => '',
229  
230 path => sub { [ "+word", 1 + int rand 6, "/" ] },
231  
232 httpurl => [ map { [ $_ ] } split m/\n/, <<_END_ ],
233 http://+hostname+path
234 http://+hostname:+port+path
235 _END_
236 port => sub { int rand(1 + (2 ** 15)) },
237  
238 mailto => \"mailto:+email",
239  
240 tld => "topleveldomain",
241  
242 topleveldomain => [ split m/\s/, <<_END_ ],
243 com org net
244 _END_
245  
246 domain => "domainname",
247  
248 domainname => [ map { [ $_ ] } split m/\n/, <<_END_ ],
249 example.+tld
250 _END_
251  
252 host => "hostname",
253  
254 hostname => [ map { [ $_ ] } split m/\n/, <<_END_ ],
255 +word.+domainname
256 +domainname
257 _END_
258  
259 );
260  
261 use constant MAXIMUM_RECURSION => 2 ** 12;
262 use constant GENERATOR => \%GENERATOR;
263  
264 =head1 EXPORT
265  
266 =cut
267  
268 our @EXPORT_OK = qw(lorem);
269  
270 =head1 FUNCTIONS
271  
272 =head2 new [$source]
273  
274 Construct a new Text::Lorem::More object
275  
276 =cut
277 sub new {
278 my $self = bless {}, shift;
279 my $source;
280 if (@_) {
281 if (ref $_[0] eq "HASH") {
282 my $generator = shift;
283 my $priority = shift;
284 $source = new Text::Lorem::More::Source($source, $priority) if ref $source eq "HASH";
285 }
286 elsif (UNIVERSAL::isa($_[0], "Text::Lorem::More::Source")) {
287 $source = shift;
288 }
289 elsif (! defined $_[0]) {
290 $source = new Text::Lorem::More::Source;
291 }
292 }
293 else {
294 $source = $self->_DEFAULT_SOURCE->copy;
295 }
296 $self->{source} = $source;
297 return $self;
298 }
299  
300 =head2 generate $pattern [, $count, $separator]
301  
302 Generate some text using the specified pattern.
303  
304 C<generate> is faster than C<process>, as C<generate> uses regex to perform substitution.
305  
306 In list context, return a list with C<$count> number of "words"
307  
308 In scalar context, return C<$pattern> repeated C<$count> times and joined by C<$separator>.
309  
310 B<If you do not specify scalar context on the receiving end, then the separator will simply be discarded.>
311 B<This may change in the future>
312  
313 The default for C<$count> is 1.
314  
315 The default for C<$separator> is " ".
316  
317 A pattern will usually contain one or more generator tokens. For example:
318  
319 +name
320 +fullname
321 +word+word+word+word
322  
323 The pattern can also contain other text:
324  
325 +name@+domainname
326 "+firstname +lastname"
327  
328 Sometimes you might need to enclose the token identifier between C<+{> and C<}>
329  
330 For example, the following pattern won't work right:
331  
332 prefix+namesuffix
333  
334 But this will:
335  
336 prefix+{name}suffix
337  
338 If you need to include a '+' in your pattern, you'll have to use C<process> instead.
339  
340 =cut
341 sub generate {
342 my $self = shift;
343 # _generate may recurse any number of times.
344 # the RECURSION counter makes sure it doesn't get out of hand.
345 local $Text::Lorem::More::RECURSION = 0;
346 return $self->_generate(@_);
347 }
348  
349 =head2 process $text
350  
351 Process a block of text, performing pattern substitutions as they're found.
352  
353 process is slower than generate, as process uses L<Parse::RecDescent>.
354  
355 To escape '+', simply repeat it. For example, to produce "2 + 2" you would submit:
356  
357 2 ++ 2
358  
359 Please see C<generate> for more information.
360  
361 =cut
362 sub process {
363 my $self = shift;
364 # _process may recurse any number of times.
365 # the RECURSION counter makes sure it doesn't get out of hand.
366 local $Text::Lorem::More::RECURSION = 0;
367 return $self->_process(@_);
368 }
369  
370 =head2 source
371  
372 Return the generator source for this instance.
373  
374 =cut
375 sub source {
376 my $self = shift;
377 return $self->{source};
378 }
379  
380 =head2 lorem
381  
382 A L<Text::Lorem::More> singleton.
383  
384 =cut
385 sub lorem() { __PACKAGE__->_singleton }
386  
387 sub _DEFAULT_SOURCE {
388 return our $SOURCE ||= do {
389 $SOURCE = new Text::Lorem::More::Source;
390 $SOURCE->push(GENERATOR);
391 $SOURCE;
392 }
393 }
394  
395 sub AUTOLOAD {
396 my $self = shift->_self;
397 my $method = our $AUTOLOAD;
398 $method =~ s/.*:://;
399  
400 if ($self->{source}->find($method)) {
401 no strict 'refs';
402 *$AUTOLOAD = sub { my $self = shift; $self->generate("+$method", @_) };
403 unshift @_, $self;
404 goto &$AUTOLOAD;
405 }
406 else {
407 carp "no such generatelet for \"$method\" found";
408 return "";
409 }
410 }
411  
412 our ($RECURSION, $COUNT, $PRUNE);
413 sub _process {
414 my $self = shift;
415 my $content = shift;
416 my $count = shift;
417 my $separator = shift;
418  
419 $RECURSION += 1;
420 croak "Too much recursion ($RECURSION) on \"$content\"" if $RECURSION >= MAXIMUM_RECURSION;
421  
422 $count = 1 unless defined $count;
423 croak "\$count ($count) should be a number" unless $count =~ m/^\d+$/;
424  
425 $separator = " " unless defined $separator;
426  
427 local $Text::Lorem::More::COUNT = $count; $COUNT = $COUNT;
428 local $Text::Lorem::More::PRUNE = 0;
429  
430 my @content;
431 while ($count >= 0) {
432 my $yield = "";
433 my $parseryield = $PARSER->content($content);
434 for (@$parseryield) {
435 $yield .= ref $_ ? $$_ : $self->_replace_pattern($_);
436 }
437 push @content, $yield if $Text::Lorem::More::PRUNE;
438 last if 0 == $count || $Text::Lorem::More::PRUNE;
439 push @content, $yield;
440 $count--;
441 }
442  
443 return $content[0] if 1 == @content;
444 return wantarray ? @content : join $separator, @content;
445 }
446  
447 sub _generate {
448 my $self = shift;
449 my $pattern = shift;
450 my $count = shift;
451 my $separator = shift;
452 my $fast = shift;
453  
454 $RECURSION += 1;
455 croak "Too much recursion ($RECURSION) on \"$pattern\"" if $RECURSION >= MAXIMUM_RECURSION;
456  
457 $count = 1 unless defined $count;
458 croak "\$count ($count) should be a number" unless $count =~ m/^\d+$/;
459  
460 $separator = " " unless defined $separator;
461  
462 local $Text::Lorem::More::COUNT = $count; $COUNT = $COUNT;
463 local $Text::Lorem::More::PRUNE = 0;
464  
465 my @content;
466 while ($count >= 0) {
467 my $pattern = $pattern;
468 $pattern =~ s/\+\{(\w+)\}|\+(\w+)/$self->_replace_pattern($1 || $2)/eg;
469 push @content, $pattern if $Text::Lorem::More::PRUNE;
470 last if 0 == $count || $Text::Lorem::More::PRUNE;
471 push @content, $pattern;
472 $count--;
473 }
474  
475 return $content[0] if 1 == @content;
476 return wantarray ? @content : join $separator, @content;
477 }
478  
479 sub _replace_pattern {
480 my $self = shift;
481 my $pattern = shift;
482  
483 my $generatelet = $self->{source}->find($pattern);
484  
485 return $pattern unless $generatelet;
486  
487 my $content;
488 if (ref $generatelet eq "ARRAY") {
489 $content = $generatelet->[int rand @$generatelet];
490 }
491 elsif (ref $generatelet eq "SCALAR") {
492 $content = $$generatelet;
493 }
494 elsif (ref $generatelet eq "CODE") {
495 $content = $generatelet->($self);
496 }
497 else {
498 croak "Don't know how to run/handle generatelet \"$generatelet\"";
499 }
500  
501 if (ref $content eq "ARRAY") {
502 my $filter;
503 $filter = shift @$content if ref $content->[0] eq "CODE";
504 my ($pattern, $count, $separator) = @$content;
505 local $_ = $self->_generate($pattern, $count, $separator);
506 $_ = $filter->($_) if $filter;
507 $content = $_;
508 }
509  
510 return $content;
511 }
512  
513 sub _self($) { return ref $_[0] ? $_[0] : $_[0]->_singleton }
514  
515  
516 sub _singleton {
517 my $class = shift;
518 return our $singleton ||= $class->new;
519 }
520  
521 sub DESTROY {
522 }
523  
524 =head1 AUTHOR
525  
526 Robert Krimen, C<< <robertkrimen at gmail.com> >>
527  
528 =head1 SEE ALSO
529  
530 L<Text::Lorem>
531  
532 L<WWW::Lipsum>
533  
534 L<http://lipsum.com/>
535  
536 =head1 SOURCE
537  
538 You can contribute or fork this project via GitHub:
539  
540 L<http://github.com/robertkrimen/text-lorem-more/tree/master>
541  
542 git clone git://github.com/robertkrimen/text-lorem-more.git Text-Lorem-More
543  
544 =head1 ACKNOWLEDGEMENTS
545  
546 Thanks to Adeola Awoyemi for writing L<Text::Lorem>
547  
548 =head1 COPYRIGHT & LICENSE
549  
550 Copyright 2006 Robert Krimen, all rights reserved.
551  
552 This program is free software; you can redistribute it and/or modify it
553 under the same terms as Perl itself.
554  
555 =head1 BUGS
556  
557 Probaby a lot. Please report them (as below) and I'll take a look.
558  
559 Please report any bugs or feature requests to
560 C<bug-text-lorem-more at rt.cpan.org>, or through the web interface at
561 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Lorem-More>.
562 I will be notified, and then you'll automatically be notified of progress on
563 your bug as I make changes.
564  
565 =head1 SUPPORT
566  
567 You can find documentation for this module with the perldoc command.
568  
569 perldoc Text::Lorem::More
570  
571 You can also look for information at:
572  
573 =over 4
574  
575 =item * AnnoCPAN: Annotated CPAN documentation
576  
577 L<http://annocpan.org/dist/Text-Lorem-More>
578  
579 =item * CPAN Ratings
580  
581 L<http://cpanratings.perl.org/d/Text-Lorem-More>
582  
583 =item * RT: CPAN's request tracker
584  
585 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-Lorem-More>
586  
587 =item * Search CPAN
588  
589 L<http://search.cpan.org/dist/Text-Lorem-More>
590  
591 =back
592  
593 =cut
594  
595 1; # End of Text::Lorem::More