in reply to Storing multiple blocks of text in the __DATA__ section

1. You should be aware that <<ENDS is dealt like <<"ENDS" i.e. allows variable interpolation. Better write <<'ENDS' to avoid this.

2. you are free to use multiple here-docs in the same line, so if the number of blocks is too small for concerns about being DRY, you can write

use Data::Dump; my %desc; init_desc(); dd \%desc; sub init_desc { @desc{ONE,TWO}= (<<'__ONE__',<<'__TWO__'); one __ONE__ two __TWO__ }

3. Please note that I've put the population part away into a sub init_desc() , like this you can have multiple of such initializations hidden at the end of your code.

4. Aforementioned solution isn't as DRY as you wanted, but actually your split-solution wasn't too bad, though your grep to ignore the first line is dangerous:

my %desc = init_desc(); dd \%desc; sub init_desc { (undef,my %hash) = # ignore +first line split /^ \[ (\w+) \] \s* $/xm, <<'__ENDS__'; [ONE] one [TWO] two __ENDS__ return %hash; }

(potential trimming of leading and trailing "\n" is left as an exercise).

5. please note that the last approach can also be used to parse a slurped __DATA__ section.

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: Storing multiple blocks of text in the __DATA__ section
by blindluke (Hermit) on Jan 03, 2015 at 14:35 UTC

    Thank you for taking the time to reply, and providing all those suggestions.

    Ad 1: Noted. I expect that due to this behavior, even if I wanted interpolation, it would still be better ( in terms of style) to write <<"ENDS" to make the fact (that I am aware of this) explicit.

    Ad 2&3: Excellent stuff, I was not aware of this possibility (multiple here-docs in the same line). Thank you!

    Ad 4: Very interesting. I assume that the danger associated with grep is the fact that it will remove both undef (as intended) and anything that evaluates as non-true (could pose a problem). Is there any other danger involved? Not that it would matter much, as your solution is definitely better, but I'm just curious.

    Ad 5: Slurping __DATA__ is exactly what I had in mind when asking about possible split usage.

    - Luke