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:

In reply to Best way to read a file into memory and use normal operation on memory file? 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.