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 ☆☆☆☆ :)


In reply to Re: Storing multiple blocks of text in the __DATA__ section by LanX
in thread Storing multiple blocks of text in the __DATA__ section by blindluke

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.