in reply to Storing multiple blocks of text in the __DATA__ section
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 |