Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^2: Redirecting/Restoring of Memory Files by ELISHEVA
in thread Redirecting/Restoring of Memory Files by ELISHEVA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2021-10-24 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?