in reply to Re: Redirecting/Restoring of Memory Files
in thread Redirecting/Restoring of Memory Files
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Redirecting/Restoring of Memory Files
by flexvault (Monsignor) on Jun 25, 2015 at 18:42 UTC |
In Section
Seekers of Perl Wisdom