1: <code>
2: # This is a simple script that dumps a hexadecimal
3: # representation of a file
4: # It was origginally written in C, so it suffers from some
5: # C syntax (mostly `for' loops could probably be redone
6: # in a more `perlish' way and the use of ternary operator
7: # ?: is quite ugly). It also overuses printf's where a more
8: # elegant solution with `unpack' could be found.
9: # Never-the-less, it did the job that I needed to do, so
10: # here it is for all to see (and maybe even use).
11:
12: use strict;
13: use warnings;
14: $#ARGV == 0 or die 'Usage: hexdump.pl file';
15: open my $inpt, "<$ARGV[0]" or die "Can't open $ARGV[0]";
16: binmode $inpt; # for DOS and the rest...
17: for(my $line_no = 0; my $i = read $inpt, $_, 16; $line_no++)
18: {
19: push my @a, split //; # put things onto char array
20: printf "%05x ", $line_no; # print line number
21: # print hex codes
22: printf "%02x ", ord() & 0xff foreach @a;
23: # pad last line if neccessary
24: for(my $j = $#a; $j < 15; $j++) { print ' '; }
25: # if a character is a control character, print a dot,
26: # else print normal ascii char
27: (ord() < 32 or ord() == 127) ? print "." : print foreach @a;
28: print "\n";
29: }
30: close $inpt;
31: </code>