Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Is a here-doc a good way to retrieve data file contents for testing?

by haukex (Bishop)
on Jan 17, 2021 at 10:03 UTC ( #11127032=note: print w/replies, xml ) Need Help??


in reply to Is a here-doc a good way to retrieve data file contents for testing?

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

Replies are listed 'Best First'.
Re^2: Is a here-doc a good way to retrieve data file contents for testing?
by Lotus1 (Vicar) on Jan 18, 2021 at 01:18 UTC
    Also note that if you need filehandles[...]

    I have wanted to do this before but didn't know about this feature. Thank you!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2021-08-01 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (13 votes). Check out past polls.

    Notices?