I was using XML::Simple to parse a document and was told "junk after document element at line 77, column 0, byte 2910. So I wrote the following program to help me take a look at the data in the file around that byte position.

Does anyone have any input into whether I am using the right method of doing byte-level analysis of a file for bad data? Program output *precedes* the source code.

Program Output Starts on Next Line

< 47 / < 99 c < 100 d < 62 > * 10 > 60 < > 99 c > 100 d > 62 >

Program

#!/usr/bin/perl use strict; my $f = shift || die "must supply filename"; open F, $f or die "couldnt open $f: $!"; my $bytepos = shift || die "must supply bytepos"; my $offset = 4; my @range = ($bytepos-$offset .. $bytepos+$offset); my $range = @range; my $text = join '', <F>; my @substr = ($range[0]-1, $range); warn "substr @substr"; #my $chunk = substr $text, @substr; my $chunk = substr $text, $range[0]-1, $range; warn substr $chunk, 0, 20; my $format = "C" . $range; my @unpack = unpack $format, $chunk; my $normal = $range[0]; my @chunk = split //, $chunk; for (@range) { my $arydex = $_-$normal; if ($_ < $bytepos) { print '< '; } if ($_ > $bytepos) { print '> '; } if ($_ == $bytepos) { print '* '; } print unpack 'C', $chunk[$arydex]; print "\t"; print $chunk[$arydex]; print "\n"; }

In reply to Byte-level file inspection? by princepawn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.