PhilHibbs has asked for the wisdom of the Perl Monks concerning the following question:
This script is in a .cmd file so it can be invoked directly from the Command Prompt. Ignore everything up to the use strict; as this is a standard Perl/Batch polyglot.
@rem = '--*-Perl-*-- @echo off perl "%~dpnx0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl @rem '; #!perl #line 8 use strict; use warnings; binmode(STDIN); $/ = \16; my $line = ''; my $len = 0; my $offset=0; my $offhex=''; my $ord=0; my $hex=''; my $char=''; print <<HEAD; Offset 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789A +BCDEF -------- ----------------------------------------------- ----------- +----- HEAD format STDOUT = @<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<< +<<<<< $offhex, $hex, $char, . while (<>) { $len = length; $offhex = sprintf('%08X', $offset); $hex = ''; $char = ''; foreach (split (//)){ $ord = ord($_); $hex .= sprintf('%02X ', $ord); $char .= ($ord > 13 or $ord < 7) ? $_ : "."; } write; $offset += $len; } __END__ :endofperl
Update: If I set the environment variable PERLIO=:raw as per ysth's suggestion, it works for files passed as parameters. This breaks redirected input, though! So, my script just tests %1 and only sets the PERLIO variable if there is a parameter.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: binmode(STDIN)
by tye (Sage) on Feb 08, 2005 at 18:08 UTC | |
by ysth (Canon) on Feb 09, 2005 at 03:10 UTC | |
|
Re: binmode(STDIN)
by nobull (Friar) on Feb 08, 2005 at 18:13 UTC | |
by PhilHibbs (Hermit) on Feb 08, 2005 at 18:15 UTC | |
|
Re: binmode(STDIN)
by ysth (Canon) on Feb 09, 2005 at 03:15 UTC | |
by PhilHibbs (Hermit) on Feb 10, 2005 at 09:55 UTC | |
by PhilHibbs (Hermit) on Feb 10, 2005 at 14:15 UTC | |
|
Re: binmode(STDIN)
by BrowserUk (Patriarch) on Feb 08, 2005 at 19:24 UTC | |
by tye (Sage) on Feb 09, 2005 at 00:51 UTC | |
by BrowserUk (Patriarch) on Feb 09, 2005 at 03:07 UTC |