Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
For testing purposes I would like my test function to return exactly what the file contains.

For tests, I think either of what you showed is ok, plus the variations shown by the others. Here are my comments on various ways to get multiline data, in particular data for tests:

  • Here docs are probably one of the most common ways to get multiline strings. One minor potential caveat is that line endings in here docs are always \n's. If you want to explicitly test different line endings, then I recommend qq strings instead and explicit backslash escapes (\n, \r, etc.) instead of literal line breaks. Also, if indented here docs are desired, properly unindenting them in Perl before 5.26 requires a bit of extra code.

  • Multiline strings work, although PBP recommends against them (ProhibitImplicitNewlines).

  • __DATA__ is another alternative, one potential caveat to remember here is that DATA is the filehandle that is used to read the Perl source file, so for example if you've got use utf8;, that remains enabled when reading from __DATA__ (unless changed with binmode). Plus, if you want to get multiple multiline strings from __DATA__, you'll have to split them yourself or use a module for that (e.g. Inline::Files, though that's a fairly powerful module).

  • Of course, you can always use actual files. Personally I often generate them using functions like this, and as mentioned above, using qq{} strings and backslash escapes so that I have full control over what the files will look like.

  • Though I wouldn't recommend this for embedded test data, sometimes there may be instances where you want to test something that's part of the POD documentation. For example, as part of my author tests I often test code snippets from my POD like this (I've even written a fairly complex parsing module, though I now think that's only worth it if there's a lot of code in the POD).

Also note that if you need filehandles, you can use in-memory opens, which are relatively performant at splitting strings into lines.

open my $fh, '<', \<<'EOF' or die $!; Hello World EOF

In reply to Re: Is a here-doc a good way to retrieve data file contents for testing? by haukex
in thread Is a here-doc a good way to retrieve data file contents for testing? by Lotus1

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 lurking in the Monastery: (3)
As of 2021-09-17 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?