#! /usr/local/bin/perl

use strict;
my ($case, $digit, $fillup, $letter);

user_input();
draw_matrix();

sub user_input {
    INPUT:

    print 'Please input an odd number >= 3: ';
    chomp ($digit = <STDIN>);
    print 'Please input a letter (a-z/A-Z): ';
    chomp ($letter = <STDIN>);
    print 'Fillup char: ';
    chomp ($fillup = <STDIN>);

    if ($digit < 3) {
        print "\n$digit too small.\n";
        goto INPUT;
    } 
    elsif ($digit % 2 == 0) {
        print "\n$digit not an odd number.\n";
        sleep (1);
        goto INPUT;
    } 
    elsif ($letter !~ /[a-z]/i) {
        print "\n$letter not a letter.\n";
        sleep (1);
        goto INPUT;
    }

    $case = $letter =~ /[a-z]/ ? 'lower' : 'upper';
}

sub draw_matrix {
    my $times = 1;
    print "\n";

    for (my $i = 1; $i <= $digit; $i++) {
        if ($i < ($digit / 2)) {
            print $fillup x ( ($digit - $times) / 2);

            if (defined ($letter) ) { 
                print $letter x $times; 
            } 
            else { 
                print $i x $times; 
            }

            print $fillup x ( ($digit - $times) / 2);
            print "\n";

            $times += 2;
        } else {
            print $fillup x ( ($digit - $times) / 2);

            if (defined ($letter) ) { 
                print $letter x $times; 
            } 
            else {
                print $i x $times; 
            }

            print $fillup x ( ($digit - $times) / 2);
            print "\n";

            $times -= 2;
        }

        if ( defined( $letter ) ) {
            $letter = chr( ord( $letter ) + 1 );

            if ($letter !~ /[a-z]+/i) { 
                $letter = $case eq 'lower' ? 'a' : 'A';
            }
        }
    }
}