Acme-Text-Rhombus
view release on metacpan or search on metacpan
lib/Acme/Text/Rhombus.pm view on Meta::CPAN
$lines++ if $lines % 2 == 0;
my ($line, $repeat, $rhombus);
for ($line = $repeat = 1; $line <= $lines; $line++) {
my $spaces = ($lines - $repeat) / 2;
$rhombus .= $fillup x $spaces;
$rhombus .= $char x $repeat;
$rhombus .= $fillup x $spaces;
$rhombus .= "\n";
$repeat = $line < ($lines / 2) ? $repeat + 2 : $repeat - 2;
if ($is_letter) {
$char = $forward ? chr(ord($char) + 1) : chr(ord($char) - 1);
}
elsif ($is_digit) {
$char = $forward ? $char + 1 : $char - 1;
}
elsif ($is_random) {
$char = $chars[int(rand(@chars))];
}
if ($is_letter && $char !~ /[a-zA-Z]/) {
$char = $alter{$case}->($forward ? 'a' : 'z');
}
elsif ($is_digit and $char > 9 || $char < 0) {
$char = $forward ? 0 : 9;
}
}
return $rhombus;
}
sub rhombus { return rhombus_letter(@_); }
sub rhombus_letter
{
my %opts = @_;
my $lines = $get_opt->(\%opts, 'lines', qr/^\d+$/, LINES);
my $letter = $get_opt->(\%opts, 'letter', qr/^[a-zA-Z]$/, 'a');
my $case = $get_opt->(\%opts, 'case', qr/^(?:low|upp)er$/, 'upper');
my $fillup = $get_opt->(\%opts, 'fillup', qr/^\S$/, FILLUP);
my $forward = $get_opt->(\%opts, 'forward', qr/^[01]$/, FORWARD);
return _draw_rhombus('letter', $lines, $letter, $case, $fillup, $forward);
}
sub rhombus_digit
{
my %opts = @_;
my $lines = $get_opt->(\%opts, 'lines', qr/^\d+$/, LINES);
my $digit = $get_opt->(\%opts, 'digit', qr/^\d$/, 0);
my $fillup = $get_opt->(\%opts, 'fillup', qr/^\S$/, FILLUP);
my $forward = $get_opt->(\%opts, 'forward', qr/^[01]$/, FORWARD);
return _draw_rhombus('digit', $lines, $digit, undef, $fillup, $forward);
}
sub rhombus_random
{
my %opts = @_;
my $lines = $get_opt->(\%opts, 'lines', qr/^\d+$/, LINES);
my $fillup = $get_opt->(\%opts, 'fillup', qr/^\S$/, FILLUP);
return _draw_rhombus('random', $lines, undef, undef, $fillup, undef);
}
1;
__END__
=head1 NAME
Acme::Text::Rhombus - Draw a rhombus with letters/digits
=head1 SYNOPSIS
use Acme::Text::Rhombus qw(rhombus);
print rhombus(
lines => 15,
letter => 'c',
case => 'upper',
fillup => '.',
forward => 1,
);
__OUTPUT__
.......C.......
......DDD......
.....EEEEE.....
....FFFFFFF....
...GGGGGGGGG...
..HHHHHHHHHHH..
.IIIIIIIIIIIII.
JJJJJJJJJJJJJJJ
.KKKKKKKKKKKKK.
..LLLLLLLLLLL..
...MMMMMMMMM...
....NNNNNNN....
.....OOOOO.....
......PPP......
.......Q.......
=head1 FUNCTIONS
=head2 rhombus, rhombus_letter
Draws a rhombus with letters and returns it as a string.
If no option value is supplied or if it is invalid, then a default
will be silently assumed (omitting all options will return a rhombus
of 25 lines).
Given that the specified number of lines is even, it will be
incremented to satisfy the requirement of being an odd number.
Options:
=over 4
=item * C<lines>
Number of lines to be printed. Defaults to 25.
=item * C<letter>
( run in 4.097 seconds using v1.01-cache-2.11-cpan-d80b1682f3f )