Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: sending data thru a sub routine

by GrandFather (Saint)
on May 12, 2014 at 02:08 UTC ( [id://1085736]=note: print w/replies, xml ) Need Help??


in reply to sending data thru a sub routine

A few general tips:

  1. Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).
  2. Declare variables in the smallest sensible scope and don't initialise them with a bogus value.
  3. Avoid unless and until. They invert the sense of their expression and often cause confusion.
  4. Don't use a regular expression match where a string compare is intended. $fileLocation =~ 'SCE' is not the same as $fileLocation eq 'SCE'!
  5. If you are dealing with binary data use unpack and pack.
Perl is the programming world's equivalent of English

Replies are listed 'Best First'.
Re^2: sending data thru a sub routine
by james28909 (Deacon) on May 12, 2014 at 02:32 UTC
    if you open this file in a hex editor, i am dealing with binary and plain text. $filename comes from plain text. $filelocation and $filesize comes from binary in the hex editor. 0x00, read 8 bytes = $filelocation, read 8 more bytes, $filesize, read 32 more bytes, $filename. repeat from current position.
    and thank you for the pointers :)
      "... 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
        'The end of the world is neigh '

        So Eliot should have written "This is the way the world ends/Not with a bang but a whinny"?

        i love regex xD

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1085736]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found