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.


In reply to Re^2: Best way to read a file into memory and use normal operation on memory file? by AppleFritter
in thread 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.