#!perl

# DATE
# VERSION

use strict;
use warnings;

use Getopt::Long;

my %Opts = (
    base => 'dec',
);
GetOptions(
    'hex'   => sub { $Opts{base} = 'hex'   },
    'octal' => sub { $Opts{base} = 'octal' },
);

my $header;
my $chars_per_row;
my $start;
my $fmt;

if ($Opts{base} eq 'dec') {
    $header = "    0 1 2 3 4 5 6 7 8 9";
    $chars_per_row = 10;
    $start = 30;
    $fmt = "%3d ";
} elsif ($Opts{base} eq 'hex') {
    $header = "   0 1 2 3 4 5 6 7 8 9 A B C D E F";
    $chars_per_row = 16;
    $start = 32;
    $fmt = "%2x ";
} elsif ($Opts{base} eq 'octal') {
    $header = "    0 1 2 3 4 5 6 7";
    $chars_per_row = 8;
    $start = 32;
    $fmt = "%3o ";
}

my $ord = $start;
print $header, "\n";
TABLE:
while (1) {
    printf $fmt, $ord;
    for (0..$chars_per_row-1) {
        last TABLE if $ord > 127;
        if ($ord >= 32) {
            print chr($ord), " ";
        } else {
            print "  ";
        }
        $ord++;
    }
    print "\n";
}
print "\n";

1;
#ABSTRACT: Print ASCII table
#PODNAME:

=head1 DESCRIPTION

 % asciitable
     0 1 2 3 4 5 6 7 8 9
  30       ! " # $ % & '
  40 ( ) * + , - . / 0 1
  50 2 3 4 5 6 7 8 9 : ;
  60 < = > ? @ A B C D E
  70 F G H I J K L M N O
  80 P Q R S T U V W X Y
  90 Z [ \ ] ^ _ ` a b c
 100 d e f g h i j k l m
 110 n o p q r s t u v w
 120 x y z { | } ~

 % asciitable --hex
    0 1 2 3 4 5 6 7 8 9 A B C D E F
 20   ! " # $ % & ' ( ) * + , - . /
 30 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
 40 @ A B C D E F G H I J K L M N O
 50 P Q R S T U V W X Y Z [ \ ] ^ _
 60 ` a b c d e f g h i j k l m n o
 70 p q r s t u v w x y z { | } ~
 80

 % asciitable --oct
     0 1 2 3 4 5 6 7
  40   ! " # $ % & '
  50 ( ) * + , - . /
  60 0 1 2 3 4 5 6 7
  70 8 9 : ; < = > ?
 100 @ A B C D E F G
 110 H I J K L M N O
 120 P Q R S T U V W
 130 X Y Z [ \ ] ^ _
 140 ` a b c d e f g
 150 h i j k l m n o
 160 p q r s t u v w
 170 x y z { | } ~
 200


=head1 OPTIONS

=head2 --hex

=head2 --oct

=cut
