Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Redirecting/Restoring of Memory Files

by flexvault (Monsignor)
on Jun 25, 2015 at 15:16 UTC ( #1131975=note: print w/replies, xml ) Need Help??


in reply to Redirecting/Restoring of Memory Files

Hello ELISHEVA,

I'm not sure I understand what you are trying to do. Your description implies to me that you are trying to save a file handle for later use, but your trying to use I/O to a scalar in memory.

my ($s1, $s2) = ('',''); . . . open(my $fh1, '>', \$s1) or die "Can't open fh1";
You are opening a defined but empty string variable by passing a reference to the string. This maybe a new feature of Perl that I'm not aware of, but usually you open a file on an external media.

More clarification will be needed to give a better answer, but if you want to save the file handle ( $fh1 ) for later use, you could use a hash or array, which will preserve the definitions of $fh1.

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

Replies are listed 'Best First'.
Re^2: Redirecting/Restoring of Memory Files
by ELISHEVA (Prior) on Jun 25, 2015 at 15:34 UTC

    Perl does have a feature of being able to open a file handle/stream on a string buffer rather than external media. There are several reasons why it is sometimes useful to treat a string as if it were a file. Three reasons that come to mind:

    • testing - you want to verify that a subroutine that normally sends output to STDERR , STDOUT, or even a file is in fact sending the expected output. By temporarily redirecting the stream to a string, you can examine and test the output more easily
    • simplification of API/maintenance - you are writing a program that needs to work with both file streams and data stored in strings and want one API/programming metaphor for both - either because it makes the API easier to learn or because you simply don't want to write two versions of code, one for strings and one for data read in from a file.
    • performance tuning - trading memory for speed for temp files. Read/write access on strings is obviously more memory intensive than storing the same data in a file. On the other hand, read/writes to disk are sometimes slower. Being able to switch between writing to an actual temp file vs. string and switch easily between the two makes benchmarking and performance tuning easier.

    For more information on this feature of Perl, see open and search for the phrase "Perl scalars". I believe support has been part of the Perl core since 5.8 (which is why I presume Disciplus tested that version - which is really old). See also IO::Scalar and IO::String. IO::Scalar can be used to bring support for in memory/scalar backed file handles to even earlier versions of Perl.

      Thank you ELISHEVA,

      It's always good to learn something new, and doing I/O to a string is new to me. Since I use 'diff' a lot in *nix and also have my own editor for working with development files, I'm not sure how often I would use it.

      As far as your 'black hole', you'll see the same with a regular file. I have prematurely closed a file handle and then later added a 'print' statement. No error, no warning and no data. Also if you save a file handle to disk and then retrieve, you need some 'Perl magic' to get it to be valid file handle again.

      Thanks for the updated information...Ed

      Regards...Ed

      "Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1131975]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2021-10-22 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (85 votes). Check out past polls.

    Notices?