Your file needs to have a multiple of sizeof(int) bytes. It probably doesn't.


At a glance, I see three bugs:

@bufAry = unpack('I*', $buf); assumes the buffer contains a multiple of sizeof(int) bytes.

Similarly, your output loop always starts writing from the start of the buffer, even if those bytes have already been written. Why not just use print?

my @bufAry = []; places a reference to an anonymous array in the array. That makes no sense. Good thing you overwrite this before using @bufAry. Remove the incorrect and useless initialization.

Furthermore,

The lack of use strict; or equivalent is sad. ALWAYS use this.

There's also the useless undef($buf);. Actually, it's worse than useless. It forces the string buffer to be deallocated, only to have it re-allocated on the next time. This is a waste of CPU, and it can fragment your memory.

You're needlessly using global variables for your handles. Don't do that. Use lexical variables like you do everywhere else. We're not in the 1990s.

Avoid two-arg open. Again, we're not in the 1990s.

Finally, I also question if I is the right format, since it's not the same size on every machine. See Mini-Tutorial: Formats for Packing and Unpacking Numbers.


In reply to Re: binmode copy loses final byte by ikegami
in thread binmode copy loses final byte by aplonis

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.