in reply to Re: Best way to read a file into memory and use normal operation on memory file?
in thread Best way to read a file into memory and use normal operation on memory file?
A few comments. First of all:
this above code will successfully read a file then byte reverse it to the other file, but you have to have a file on disk to do it.
Unfortunately, it won't. The code you posted will take a file, split it into chunks of 4096 byte, attempt to reverse each chunk, and then write the chunks to a new file in the same order. Worse, it fails to even reverse each chunk. Try generating a test file with several chunks, e.g. as follows:
$ for i in `seq 1 256`; do echo -n '0123456789ABCDEF' >>file; done $ for i in `seq 1 256`; do echo "This won't work" >>file; done $
Try running your script on the resulting file; you'll end up with something like the following:
1032547698BADCFE1032547698BADCFE1032547698BADCFE... khTsiw not'w ro khTsiw not'w ro khTsiw not'w ro ...
Which is not "reversed" in even the loosest sense of the word.
Now, on to your second question. This
syswrite ( $out_file, $mem_file );
simply isn't going to work, as you've found out. syswrite expects to be passed data (in the form of a scalar), but what you're passing is a reference to a filehandle, which stringifies to something along the lines GLOB(0x8006c228).
Furthermore, I'm a little puzzled as to why you want to use an in-memory file to begin with. Your first script -- if you were to fix it -- would be useful since you could use it to reverse files too large to slurp into memory all at once; with the second one, that's obviously not true anymore.
You don't need to use an in-memory file: there's a much better, easier-to-use facility for holding data in memory, namely variables. So why not do what was suggested above, using e.g. File::Slurp to read all the file's contents into a variable, and then use reverse to reverse it before writing it out again?
Frankly, I'm puzzled, but perhaps I'm missing the obvious good reason for why you're doing it this way. If that is the case, please enlighten me; otherwise, please meditate on the advice the monks gave you.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Best way to read a file into memory and use normal operation on memory file?
by james28909 (Deacon) on Jul 16, 2014 at 22:23 UTC |