#!/usr/bin/perl
#: Show what command-line arguments we've been given.
#=======================================================
#Time-stamp: "2017-06-03 66:66:66 MDT sburke@cpan.org"
require 5 || exit 3;
use strict; use warnings;

my $me = $0;
Main();
exit;

#--------------------------------------------------------------------------

sub Main {
  unless(@ARGV) {
    print "I ",
	  "(\"$me\") ",
	  "got zero arguments.\n";
    exit;
  }
  print "I ",
        "(\"$me\") ",
	"got ", (@ARGV == 1)
   ? "1 argument"
   : (@ARGV . " arguments"),
   ":\n",
  ;
  words_to_demonstrate(@ARGV);
  return
}


sub words_to_demonstrate {
  my(@arg_words) = @_;
  foreach my $word (@arg_words) {
    demonstrate_word($word);
  }
  return;
}


sub demonstrate_word {
  my($word) = @_;
  my $hexy = chars_to_hexy($word);
  printf
    "\n\t% 3s byte% 1s: [%s] %s\n", 
    length($word),
    1==length($word) ? q{} : 's',

      # Note that we don't escape the output.  Laziness, probably.

    $word,
    length($hexy) ? "\n$hexy" : '',
  ;
  return;
}

sub chars_to_hexy {
  my ($n) = @_;
  my @pairs;
  {
     my $h =  unpack "H*", $n;
     @pairs  =  $h =~ m/(..)/g;
  }

  my $out = '';
  my @lines;
  while(@pairs) { # accumulate lines
    my   @lineworth   = splice @pairs, 0, 16; # first 16
    push @lineworth, ''  while @lineworth < 16;
    push @lines, sprintf
      "\t\t%s %s %s %s  %s %s %s %s  %s %s %s %s  %s %s %s %s",
      @lineworth
    ;
  }

  return join "\n", @lines;
}

# TODO: if it looks UTF8-viable, show it also as that.


__END__

