Text-Lorem-More
view release on metacpan or search on metacpan
lib/Text/Lorem/More.pm view on Meta::CPAN
=head2 fullname
Generates a firstname and lastname separated by a space
Lorem Dicta
=head2 username
Generates a random latin word
=head2 title
Generates between 1 and 3 words with the first letter of the first word capitalized
=head2 description
Generates between 1 and 3 sentences.
=head2 tld / topleveldomain
Generates a top level domain.
Currently, this will either be "com", "org", or "net".
=head2 domain / domainname
Generates a domainname.
Currently, this will attach "example" to a tld generator result.
example.com
example.net
=head2 host / hostname
Generates a hostname.
Currently, this will either return a plain domainname, as above, or attach a latin word to a domainname result.
et.example.com
example.org
=head2 email / mail
=head2 path
=head2 httpurl
=head2 mailto
=cut
our %GENERATOR = (
name => sub { [ sub { ucfirst lc $_ }, "+word" ] },
firstname => "name",
lastname => "name",
fullname => sub { ["+firstname +lastname"] },
username => "word",
word => [ grep { length $_ } map { s/\W//g; lc } split m/\s/, <<_END_ ],
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...
_END_
title => sub { [ sub { ucfirst($_) }, "+word", 1 + int rand 3 ] },
description => sub { [ "+sentence", 1 + int rand 3 ] },
sentence => sub { [ sub { ucfirst($_) . "." }, "+word", 4 + int rand 6 ] },
paragraph => sub { [ "+sentence", 3 + int rand 4 ] },
words => sub {
$Text::Lorem::More::PRUNE = 1;
return [ "+word", $Text::Lorem::More::COUNT, " " ];
},
sentences => sub {
my $lorem = shift;
$Text::Lorem::More::PRUNE = 1;
my $count = $Text::Lorem::More::COUNT;
my @sentence;
while ($count > 0) {
push @sentence, ucfirst $lorem->words(4 + int rand 6);
$count--;
}
return join(". ", @sentence) . ".";
},
paragraphs => sub {
my $lorem = shift;
$Text::Lorem::More::PRUNE = 1;
my $count = $Text::Lorem::More::COUNT;
my @paragraph;
while ($count > 0) {
push @paragraph, $lorem->sentences(3 + int rand 4);
$count--;
}
return join("\n\n", @paragraph);
},
email => [ map { [ $_ ] } split m/\n/, <<_END_ ],
+word\@+hostname
+word\@+domainname
_END_
mail => "email",
relativepath => '',
absolutepath => '',
path => sub { [ "+word", 1 + int rand 6, "/" ] },
httpurl => [ map { [ $_ ] } split m/\n/, <<_END_ ],
http://+hostname+path
http://+hostname:+port+path
_END_
port => sub { int rand(1 + (2 ** 15)) },
mailto => \"mailto:+email",
tld => "topleveldomain",
topleveldomain => [ split m/\s/, <<_END_ ],
com org net
_END_
domain => "domainname",
domainname => [ map { [ $_ ] } split m/\n/, <<_END_ ],
example.+tld
_END_
host => "hostname",
hostname => [ map { [ $_ ] } split m/\n/, <<_END_ ],
+word.+domainname
+domainname
_END_
);
use constant MAXIMUM_RECURSION => 2 ** 12;
use constant GENERATOR => \%GENERATOR;
=head1 EXPORT
=cut
our @EXPORT_OK = qw(lorem);
=head1 FUNCTIONS
=head2 new [$source]
Construct a new Text::Lorem::More object
=cut
sub new {
my $self = bless {}, shift;
my $source;
if (@_) {
if (ref $_[0] eq "HASH") {
my $generator = shift;
my $priority = shift;
$source = new Text::Lorem::More::Source($source, $priority) if ref $source eq "HASH";
}
elsif (UNIVERSAL::isa($_[0], "Text::Lorem::More::Source")) {
$source = shift;
}
elsif (! defined $_[0]) {
$source = new Text::Lorem::More::Source;
}
}
else {
$source = $self->_DEFAULT_SOURCE->copy;
}
$self->{source} = $source;
return $self;
}
=head2 generate $pattern [, $count, $separator]
Generate some text using the specified pattern.
C<generate> is faster than C<process>, as C<generate> uses regex to perform substitution.
In list context, return a list with C<$count> number of "words"
In scalar context, return C<$pattern> repeated C<$count> times and joined by C<$separator>.
B<If you do not specify scalar context on the receiving end, then the separator will simply be discarded.>
B<This may change in the future>
The default for C<$count> is 1.
( run in 2.381 seconds using v1.01-cache-2.11-cpan-71847e10f99 )