Ok, i have been wondering this for a while, and i prob already know how to do it and i am just not realizing it, but....
if i wanted to open a file , and then byte reverse the file without writing it to disk but instead to a scalar to seek and read from, what would be the best way to do this? and without messing with the original data... or is it even possible?
like i said maybe i already know how to do this. in the end i want to open a file in memory, then byte reverse it to another scalar in memory without writing it to disk and still be able to seek and read the byte reversed file that is in memory.
something like:
use strict;
use warnings;
use File::Slurp;
my $read_file = read_file( "file" ); #read_file
my $memory_file; #declare memory file
my ( $buf, $data, $n, $bytes ); #declare variables
while (( $n = read $read_file, $data, 4096 ) != 0 ) {
print $memory_file, pack( "v*", unpack("n*", $data )); #byte rev
+erses
$bytes+=$n;
}
open ( my $write_file, ">", "reversed" ); #writes byte reversed memor
+y file to
syswrite ( $write_file, $memory_file ); #disk AFTER it byte reverse
+s in
#memory.
read ( $memory_file, $stuff, 0x200 ); #should return a value but
+doesnt
print ( $stuff );
hopefully what i am asking is clear enough, if not, just let me know :)
UPDATE, problem solved with the following code, no file::slurp needed and im pretty sure you wouldnt even need to binmode the files either.And also, just to update, this will take a 16 MB file, in 0x34 0x12 0x78 0x56 order and put it in 0x12 0x34 0x56 0x78 order.
Here is the code updated code that will seek and read as well:
open ( my $in_file, '<', "file" );
binmode( $in_file );
my $mem_file;
binmode( $mem_file );
my ( $buf, $data, $n, $bytes );
while (( $n = read $in_file, $data, 4096 ) != 0){
$mem_file .= pack( "v*", unpack("n*", $data ));
$bytes+=$n;
}
open ( my $otherMem_file, '<', \$mem_file );
binmode( $otherMem_file );
seek $otherMem_file, 0x00, 0;
read $otherMem_file, my $temp, 0x1200;
open my $out_file, '>', "reversed";
syswrite( $out_file, $temp);
Also i want to thank everyone who gave me advice, if it werent for you guys/gals here at perlmonks id be lost for sure, if i had the money id buy you all a beer :cheers: