package HexView; use strict; use warnings; BEGIN { use Exporter (); our ($VERSION, @ISA, @EXPORT); $VERSION = 0.01; @ISA = qw(Exporter); @EXPORT = qw(&HexView); } sub HexView { my $data = shift; # If a filehandle ref was an arg we convert # it to an array (in a rather ugly fashion) if (ref($data) eq 'GLOB') { @_ = <$data>; undef $data; # clean the 'GLOB(0x123456)' } while (@_){$data .= shift} my ($hex, $char); foreach (split (//,$data)){ $hex .= sprintf('%02X ', ord($_)); $char .= ord($_) > 13 ? $_ : "."; } local $: = ''; # a.k.a $FORMAT_LINE_BREAK_CHARACTERS (and we don't want that) my $formathead =<<"HEAD"; format = +--------------------------------------------------+------------------+ | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF | +--------------------------------------------------+------------------+ HEAD my $formatline = <<'LINE'; | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ^<<<<<<<<<<<<<<< | $hex, $char, LINE my $formatend = <<'END'; +--------------------------------------------------+------------------+ . END eval($formathead . $formatline x (int(length($data)/16)+1) . $formatend); write; return 1; } 1;