"... binary and plain text ..."

Hmm, not really. You are dealing with a binary file that happens to have some plain text fields. unpack makes the code easier and clearer - in this case it even makes it correct. Consider:

use strict; use warnings; (my $binStr = <<BIN) =~ s/\n//g; \x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00 The end of the world is neigh \x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00 unless you use pack and unpack BIN open my $fIn, '<', \$binStr; binmode $fIn; print "Using unpack\n"; while (read($fIn, (my $rec), 48)) { my ($fileLocL, undef, $fileSizeL, undef, $fileName) = unpack('VVVVa32', $rec); printf "Loc: %d, Size: %d, Name: '%s'\n", $fileLocL, $fileSizeL, $ +fileName; } seek $fIn, 0, 0; print "Using bogus substitution code\n"; while (!eof $fIn) { my ($fileLoc, $fileSize, $fileName); read($fIn, $fileLoc, 0x08); read($fIn, $fileSize, 0x08); read($fIn, $fileName, 0x20); $fileLoc =~ s/(.)/sprintf("%02x",ord($1))/eg; $fileSize =~ s/(.)/sprintf("%02x",ord($1))/eg; $fileName =~ s/\0+$//; printf "Loc: %d, Size: %d, Name: '%s'\n", $fileLoc, $fileSize, $fi +leName; }

Prints:

Using unpack Loc: 1, Size: 2, Name: 'The end of the world is neigh ' Loc: 3, Size: 4, Name: 'unless you use pack and unpack ' Using bogus substitution code Loc: -1, Size: -1, Name: 'The end of the world is neigh ' Loc: -1, Size: -1, Name: 'unless you use pack and unpack '

Note that I was using a build of Perl that doesn't have support for the quad word pack/unpack specification so I used the "VAX" long (32 bit) V specification and ignored the high words (that's the undefs in the variable list).

Oh, and the trailing spaces on the two "file name" lines in the sample data are important. Don't lose them copying this test script!

Perl is the programming world's equivalent of English

In reply to Re^3: sending data thru a sub routine by GrandFather
in thread sending data thru a sub routine by james28909

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.