Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening files in-memory means what?

Tags:

perl

You can open file handles in-memory ?

The in-memory part is unclear to me, what does that mean ?

If that means you can use the computer's memory, Isn't it already working like that ?

like image 984
airnet Avatar asked Aug 21 '13 20:08

airnet


People also ask

Does opening a file load it into memory?

No, it doesn't load the entire file into memory. "Opens a file" returns a handle to you allowing you to read from or write to a file.

How do I open a memory file?

To open a memory file, use the mmioOpen function with the szFilename parameter set to NULL and the MMIO_READWRITE flag set in the dwOpenFlags parameter.

Which memory file is opened for processing?

RAM (Random Access Memory) is the hardware in a computing device where the operating system (OS), application programs and data in current use are kept so they can be quickly reached by the device's processor. RAM is the main memory in a computer.


1 Answers

It means you can use filehandles to write to and read from scalar variables.

my $var = "";
open my $fh, '>', \$var;
print $fh "asdf";
close $fh;
print $var;          # asdf

Ultimately, this is just One More Way To Do

$var .= "asdf"

but there are contexts where is more convenient or more appropriate to use filehandle paradigms than string manipulation paradigms.

For example, start with this code:

open my $fh, '>', $logfile;
...
print $fh $some_message_to_be_logged;
... 500 more print $fh statements ...
close $fh;

But you know what? Now I'd rather record my log messages in a scalar variable, maybe so I can search through them, manipulate them before they are written to disk, etc. I could change all my print statements to

$logvar .= $some_message_to_be_logged

but in this case it is more convenient to just change the open statement.

open my $fh, '>', \$logvar
like image 90
mob Avatar answered Sep 22 '22 01:09

mob