in reply to Re^2: Question about binary file I/O
in thread Question about binary file I/O

I had "use strict" in the program originally, but it seemed to do nothing but cause trouble; it mostly meant that I had to put "my" before almost every variable, so I just got rid of it. I'm also not sure what you mean by lexical file handles and 3-argument open.
The "my" is actually very useful because, apart from catching typos in misspelt variable names, it limits the scope of the variable. A variable declared with "my" is known as a lexical variable because it has lexical scope; that is, its name is known from the point of declaration to the end of the block it is declared in (or end of file if not declared in a block). Hence my recommendation to use lexical file handles (e.g. my $fhin) rather than your global variable bareword file handle IN.

Using lexical file handles is better style because:

As for why the three-argument form of open is preferred, note that the old two-argument form of open is subject to various security exploits as described at Opening files securely in Perl.

See also the first four items of Perl Best Practices Chapter 10 (I/O), namely:

Replies are listed 'Best First'.
Re^6: Question about binary file I/O
by TheMartianGeek (Acolyte) on Feb 06, 2011 at 16:10 UTC
    Well, using this seemed to work:

    my $NewTileData = pack 'v', ((unpack 'v',$TileData) & 0xFC7F) | $GFXFileBits;

    Perhaps I was just misunderstanding the pack and unpack functions? The way perldoc words the explanation of them suggest that pack always returns a string and unpack always returns a list of values, neither of which I would want in this context. Thank you all for the help, in any case. (I am still confused about how pack and unpack work and what they return depending on the template, though...)
Re^4: Question about binary file I/O
by TheMartianGeek (Acolyte) on Feb 06, 2011 at 06:05 UTC
    Okay, I fixed all the scope issues and stuff. It still doesn't work, though:
    { local $/ = \2; my $TileData = 0x0000; while(my $TileData = <$fhin>) { if(length($TileData) != 2) { warn "Warning: The input file had an uneven length. Last +byte read: $TileData."; last; } my $NewTileData = (($TileData & 0xFC7F) | ($GFXFileBits)); print {$fhout} $NewTileData; } }
    $TileData is still a string instead of a number, so it doesn't work with the bitwise operators.

      $TileData is still a string instead of a number, so it doesn't work with the bitwise operators.
      Well, of course it doesn't since my suggestions related to the file handling aspects of your code only (as I thought I made clear in my original response) because BrowserUk had already answered the bitwise operator aspects of your problem. Please re-read his answer and study the documentation for pack and unpack then show us your code and explain precisely why it doesn't do what you want it to.

Re^7: Question about binary file I/O
by TheMartianGeek (Acolyte) on Mar 02, 2011 at 05:21 UTC
    Okay, I have a new file-related problem...what's the best way to read a certain number of bytes (say, 12) beginning at a certain position in a file (say, position 40), possibly reading them into an array and possibly performing operations on them, then write the result to a certain position in another file. I thought I had something, but I didn't...I don't even see a function in Perl for overwriting a string of bytes in a file, let alone know how to do the rest.

      TheMartianGeek:

      From perlvar you can read a specified amount of data by locally setting $/. You can move a file handle to a specific file location using seek, so you can do something like (untested):

      use strict; use warnings; use Fcntl qw( SEEK_SET ); . . . my @bytes; { my $input_location = 40; local $/=12; # Read 12 bytes open my $INF, '<', 'foo' or die; seek($INF, $input_location, SEEK_SET); my $t = <$INF>; @bytes = split //, $t; } # Do something with them @bytes = shuffle @bytes; # Write them to another file open my $OUF, '<+', 'bar' or die; my $output_location=1234; seek($OUF, $output_location, SEEK_SET); print $OUF, join("", @bytes);

      Generally, when I'm looking for ideas on how to do something, I scan perlfunc, perlvar and (of course) CPAN!

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      Update: Changed to SEEK_SET (which is more likely to be the useful one) and also applied seek to input file, since he mentioned it in the OP.