File: Text.pm

package info (click to toggle)
libdata-fake-perl 0.006-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 987; makefile: 7
file content (164 lines) | stat: -rw-r--r-- 4,411 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use 5.008001;
use strict;
use warnings;

package Data::Fake::Text;
# ABSTRACT: Fake text data generators

our $VERSION = '0.006';

use Exporter 5.57 qw/import/;

our @EXPORT = qw(
  fake_words
  fake_sentences
  fake_paragraphs
);

use Data::Fake::Core qw/_transform/;

my $LOREM;

#pod =func fake_words
#pod
#pod     $generator = fake_words();    # single "lorem" word
#pod     $generator = fake_words($n);  # N "lorem" words, space separated
#pod     $generator = fake_words( fake_int(1, 3) ); # random number of them
#pod
#pod Returns a generator that provides space-separated L<Text::Lorem> words as a
#pod single scalar value.  The argument is the number of words to return (or a
#pod code reference to provide the number of words); the default is one.
#pod
#pod =cut

sub fake_words {
    my ($count) = @_;
    $count = 1 unless defined $count;
    require Text::Lorem;
    $LOREM ||= Text::Lorem->new;
    return sub { scalar $LOREM->words( _transform($count) ) };
}

#pod =func fake_sentences
#pod
#pod     $generator = fake_sentences();    # single fake sentence
#pod     $generator = fake_sentences($n);  # N sentences
#pod     $generator = fake_sentences( fake_int(1, 3) ); # random number of them
#pod
#pod Returns a generator that provides L<Text::Lorem> sentences as a single
#pod scalar value.  The argument is the number of sentences to return (or a code
#pod reference to provide the number of sentences); the default is one.
#pod
#pod =cut

sub fake_sentences {
    my ($count) = @_;
    $count = 1 unless defined $count;
    return sub { "" }
      if $count == 0;
    require Text::Lorem;
    $LOREM ||= Text::Lorem->new;
    return sub { scalar $LOREM->sentences( _transform($count) ) };
}

#pod =func fake_paragraphs
#pod
#pod     $generator = fake_paragraphs();    # single fake paragraph
#pod     $generator = fake_paragraphs($n);  # N paragraph
#pod     $generator = fake_paragraphs( fake_int(1, 3) ); # random number of them
#pod
#pod Returns a generator that provides L<Text::Lorem> paragraphs as a single
#pod scalar value.  The argument is the number of paragraphs to return (or a
#pod code reference to provide the number of paragraphs); the default is one.
#pod
#pod =cut

sub fake_paragraphs {
    my ($count) = @_;
    $count = 1 unless defined $count;
    require Text::Lorem;
    $LOREM ||= Text::Lorem->new;
    return sub { scalar $LOREM->paragraphs( _transform($count) ) };
}

1;


# vim: ts=4 sts=4 sw=4 et tw=75:

__END__

=pod

=encoding UTF-8

=head1 NAME

Data::Fake::Text - Fake text data generators

=head1 VERSION

version 0.006

=head1 SYNOPSIS

    use Data::Fake::Text;

    fake_words(2)->();
    fake_sentences(3)->();
    fake_paragraphs(1)->();

=head1 DESCRIPTION

This module provides fake data generators for random words and other
textual data.

All functions are exported by default.

=head1 FUNCTIONS

=head2 fake_words

    $generator = fake_words();    # single "lorem" word
    $generator = fake_words($n);  # N "lorem" words, space separated
    $generator = fake_words( fake_int(1, 3) ); # random number of them

Returns a generator that provides space-separated L<Text::Lorem> words as a
single scalar value.  The argument is the number of words to return (or a
code reference to provide the number of words); the default is one.

=head2 fake_sentences

    $generator = fake_sentences();    # single fake sentence
    $generator = fake_sentences($n);  # N sentences
    $generator = fake_sentences( fake_int(1, 3) ); # random number of them

Returns a generator that provides L<Text::Lorem> sentences as a single
scalar value.  The argument is the number of sentences to return (or a code
reference to provide the number of sentences); the default is one.

=head2 fake_paragraphs

    $generator = fake_paragraphs();    # single fake paragraph
    $generator = fake_paragraphs($n);  # N paragraph
    $generator = fake_paragraphs( fake_int(1, 3) ); # random number of them

Returns a generator that provides L<Text::Lorem> paragraphs as a single
scalar value.  The argument is the number of paragraphs to return (or a
code reference to provide the number of paragraphs); the default is one.

=for Pod::Coverage BUILD

=head1 AUTHOR

David Golden <dagolden@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by David Golden.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004

=cut