BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Open a ram file for output, seek into it and print something and the length of the scalar changes to reflect the 'content':

open RAM, '>', \$ram; seek RAM, 10, 0; print RAM ' '; print length $ram;; 12

It also works as expected if I use binmode ':raw' on the filehandle:

open RAM, '>', \$ram; binmode RAM, ':raw' or warn $!; seek RAM, 10, 0; print RAM ' '; print length $ram;; 12

But apply ':raw' on the open, then nothing gets printed to the scalar.

open RAM, '>:raw', \$ram; seek RAM, 10, 0; print RAM ' '; print length $ram;; Use of uninitialized value in length at (eval 3) line 1. 0

Is this a restriction I've missed or a bug?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: Ram file bug?
by PodMaster (Abbot) on Feb 16, 2006 at 09:43 UTC
    Looks like you've got a bug

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Could you try again but undef $ram between the opens?

      use strict; use warnings; my $ram; open RAM, '>', \$ram; seek RAM, 10, 0; print RAM ' '; warn length $ram;; undef $ram; open RAM, '>', \$ram; binmode RAM, ':raw' or warn $!; seek RAM, 10, 0; print RAM ' '; warn length $ram;; undef $ram; open RAM, '>:raw', \$ram; seek RAM, 10, 0; print RAM ' '; warn length $ram; warn `$^X -v`; __END__ C:\test>junk 11 at C:\test\junk.pl line 9. Use of uninitialized value in open at C:\test\junk.pl line 12. 11 at C:\test\junk.pl line 16. Use of uninitialized value in length at C:\test\junk.pl line 22. 0 at C:\test\junk.pl line 22. This is perl, v5.8.6 built for MSWin32-x86-multi-thread (with 3 registered patches, see perl -V for more detail) Copyright 1987-2004, Larry Wall Binary build 811 provided by ActiveState Corp. http://www.ActiveState. +com ActiveState is a division of Sophos. Built Dec 13 2004 09:52:01 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        My results match yours

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Ram file bug?
by jmcnamara (Monsignor) on Feb 16, 2006 at 10:27 UTC

    I guess that it is a bug since the behaviour is different than when opening a physical file.

    Out of curiosity, is there any particular reason that you need to open an in-memory filehandle in ':raw' mode? I would have imagined that it would have been the behaviour anyway, even on Windows.

    --
    John.

      I would have imagined that it would have been the behaviour anyway, even on Windows

      Actually, that was exactly what I was trying to verify when I encountered the bug. Any normal file opened without specifying ':raw' gets ':crlf' by default. I just wanted to confirm that if I wrote to a ram file, then wrote the result to disk that I wouldn't get the crlf translation twice.

      Instead, I got nothing at all :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        You proabably know that you can use open to change the defaults