G'day stevieb,

I don't have MSWin available, but I can fake it sufficiently for this test with:

use open IO => ':crlf';

Here's four ways to do what you want (plus a fifth just to show what happens when one of those isn't used). There may, of course, be other ways I didn't think of.

#!/usr/bin/env perl -l use strict; use warnings; use autodie; use open IO => ':crlf'; my ($f, $fh); my $mf = \$f; my $test_file = 'pm_1144333_test_file.txt'; open $fh, '>', $mf; print $fh 'hello'; close $fh; print_raw_1('mem: ', $mf); print_raw_2('mem: ', $mf); print_raw_3('mem: ', $mf); print_raw_4('mem: ', $mf); print_raw_5('mem: ', $mf); open $fh, '>', $test_file; print $fh 'hello'; close $fh; print_raw_1('file: ', $test_file); print_raw_2('file: ', $test_file); print_raw_3('file: ', $test_file); print_raw_4('file: ', $test_file); print_raw_5('file: ', $test_file); sub print_raw_1 { my ($prompt, $file) = @_; open my $fh, '<:raw', $file; print '1. ', $prompt, unpack 'H*' while (<$fh>); close $fh; } sub print_raw_2 { my ($prompt, $file) = @_; open my $fh, '<', $file; binmode $fh, ':raw'; print '2. ', $prompt, unpack 'H*' while (<$fh>); close $fh; } sub print_raw_3 { my ($prompt, $file) = @_; use open IN => ':raw'; open my $fh, '<', $file; print '3. ', $prompt, unpack 'H*' while (<$fh>); close $fh; } sub print_raw_4 { my ($prompt, $file) = @_; use open IO => ':raw'; open my $fh, '<', $file; print '4. ', $prompt, unpack 'H*' while (<$fh>); close $fh; } sub print_raw_5 { my ($prompt, $file) = @_; open my $fh, '<', $file; print '5. ', $prompt, unpack 'H*' while (<$fh>); close $fh; }

Here's the output:

1. mem: 68656c6c6f0d0a 2. mem: 68656c6c6f0d0a 3. mem: 68656c6c6f0d0a 4. mem: 68656c6c6f0d0a 5. mem: 68656c6c6f0a 1. file: 68656c6c6f0d0a 2. file: 68656c6c6f0d0a 3. file: 68656c6c6f0d0a 4. file: 68656c6c6f0d0a 5. file: 68656c6c6f0a

And, if I hadn't "faked it", i.e. not including the use open IO => ':crlf'; line, I just get:

1. mem: 68656c6c6f0a 2. mem: 68656c6c6f0a 3. mem: 68656c6c6f0a 4. mem: 68656c6c6f0a 5. mem: 68656c6c6f0a 1. file: 68656c6c6f0a 2. file: 68656c6c6f0a 3. file: 68656c6c6f0a 4. file: 68656c6c6f0a 5. file: 68656c6c6f0a

See also:

and, if you're interested in internals:

— Ken


In reply to Re: Is there a way to open a memory file with binmode :raw? by kcott
in thread Is there a way to open a memory file with binmode :raw? by stevieb

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.