http://qs321.pair.com?node_id=302088

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

In Perl 5.8 perldoc -f open tells me:

# [...] File handles can be opened to "in memory" files held in Perl scalars via: open($fh, ">", \$variable)

Is there a way to open files in memory in Perl 5.6.1. ?

A lot of our test code does kludgy stuff like write test files to /tmp

On related note is there a good module or technique for Perl 5.8 that allows a RAM based dir ?

I have a script that tars a bunch of files. It would be nice to put them in a directory before tarring them and the end user doesn't want to create temporary files. I've taken a quick look through CPAN but nothing jumped out at me. Please feel free to point out something I've overlooked

update: By "RAM based file system" I mean soemthing like: "not on physical disc", something that nothing but root with a debugger and my program can access.

Replies are listed 'Best First'.
Re: in memory files in 5.6.1
by liz (Monsignor) on Oct 25, 2003 at 19:07 UTC
    Maybe IO::stringy is what you're looking for?

    I'm not sure what you mean with RAM based dirs, but it should be possible to override mkdir(), chdir() and rmdir(). I don't know of any module that does what you appear to want.

    Liz

      If you are on windows, just do something like:

      subst p: \perl\bin

      Then your perl bin directory becomes your p: driver. Perl will pick it just as a physical driver.

      As for in memory file, unfortunately it is not in 5.6.1. Check perldelta of 5.8.0, under Core Enhancement/Perl IO is now the default.

      Thanks Liz

      update: Thanks hanenkamp

      IO::stringy looks useful, but as far as I can tell an stringy object can't be treated exactly like a file handle.

      For example I have to invoke the print method of the object instead of printing to a file handle

        Not true, this should work:

        use IO::Scalar; my $fh = IO::Scalar->new; print $fh "foo\n"; # Rather than $fh->print ... # or, if you prefer select $fh; print "bar\n";

        This works through the magic of a tied file handle. See perltie for details on tying a file handle.

Re: in memory files in 5.6.1
by etcshadow (Priest) on Oct 26, 2003 at 00:35 UTC
    To create a "RAM based directory" is more of a task for an operating-system than a program. Really, you'd need to mount a new type of file-system (there may be support for some existing RAM-disk-based file-system in your OS or not).

    What you have the option of doing inside your program is to not go to the file-system at all, which is what various perl constructs such as IO::Scalar and the similar builtin functionality in 5.8 do.


    ------------
    :Wq
    Not an editor command: Wq
Re: in memory files in 5.6.1
by nevyn (Monk) on Oct 26, 2003 at 08:11 UTC
    Is there a way to open files in memory in Perl 5.6.1. ? A lot of our test code does kludgy stuff like write test files to /tmp

    As well as doing it directly via. IO::Stringy, you can do it indirectly via. String Buffer.

    --
    James Antill
Re: in memory files in 5.6.1
by bart (Canon) on Oct 26, 2003 at 10:45 UTC
    I have a script that tars a bunch of files. It would be nice to put them in a directory before tarring them and the end user doesn't want to create temporary files.
    If you create the archive using Archive::Tar, you don't need to store the "files" as actual, physical files first. You can just add them to the archive in memory. See add_data().

    Hmm. I'm a bit surprised to find that, if the source is indeed physical files but you want to archive it in a different location in the stored file tree, it provides no way to do just that. You can rename() the member file, after you added it, I suppose — but only if your version of the module is recent enough.

Re: in memory files in 5.6.1
by mce (Curate) on Oct 29, 2003 at 10:06 UTC
    On solaris /tmp is a "memory" filesystem (it is the swap space).

    Just my 0.02$ worth.
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium

Re: in memory files in 5.6.1
by Anonymous Monk on Aug 22, 2007 at 18:02 UTC
    Helpful use: in this example i wanted to open an in-memory file so i could make use of Perl's: format & write operations. This will print a formatted line into a "in mem" file and return it as a scalar string to the calling function. It was a easy way of making a formatted (aligned) toString() for my class. Enjoy.

    sub toString { my $self = shift; my $thishash = $self->{_listofkeys}; my $memfile; open (FORMATTED_OUT, '>',\$memfile) || warn "unable to open mem +ory file!"; format FORMATTED_OUT = IMEI BrandName Model OS + Registered To ============================================================== +============================================ @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<< +<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $thishash->{IMEI}, $thishash->{PhoneBrandName}, $thishash->{Ph +oneModel}, $thishash->{PhoneOS}, $thishash->{RegisteredTo} . write FORMATTED_OUT ; close (FORMATTED_OUT); return $memfile; }