#!/usr/bin/perl use strict; use warnings; my $file = shift or die "Usage: hexdump filename\n"; open my $input, '<', $file or die "Cannot open '$file' for reading: $!\n"; binmode $input, ':bytes'; use constant CHUNK_SIZE => 4096; use constant BYTES_PER_LINE => 0x10; READ_CHUNK: while ( defined (my $ret = sysread $input, my $buffer, CHUNK_SIZE) ) { last READ_CHUNK unless $ret; my $pos = 0; while ( $pos + BYTES_PER_LINE < $ret ) { print hexdump_line(substr $buffer, $pos, BYTES_PER_LINE), "\n"; $pos += BYTES_PER_LINE; } } sub hexdump_line { my ($row) = @_; my $output = join(' ', map { sprintf '%2.2X', ord $_ } split '', $row ); $row =~ s/[^\x20-\x7e]/\./g; $output .= (q{ }) x 4 . $row; } __END__