Enlightened Monks!

I'm storing a few descriptions within the code. Right now, it's done in a very basic way:

my %desc; $desc{'room'} =<<END; some text here, probably something a few lines long END $desc{'wall'} =<<END; another text here, probably something a few lines long [sometimes with braces] sometimes with a few paragraphs END

Honestly, I don't like it. I would much rather see it in the DATA section (and later in a file of its own), with the following (or similar) layout:

__DATA__ [room] some text here, probably something a few lines long [wall] another text here, probably something a few lines long [sometimes with braces] sometimes with a few paragraphs

Does anyone know of a Config:: module that would accept such syntax? Or a way to put something like multiple __DATA__ sections in the code, each with a name of their own?

I could parse such a section myself, with something like this:

my %desc; my $key = 'error'; for (<DATA>) { if (/^\[(\w+)\]\s*$/) { $key = $1; } else { $desc{$key} .= $_; } } die if defined $desc{'error'};

... but it seems worse than the initial solution, with direct assignments to the hash keys.

Still, maybe there is a more elegant way to parse such a DATA section.

UPDATE:

Can I use split with a regexp pattern, and use a capture within the pattern, to get the hash key?

I managed to find the answer to this one. The code below produces the desired %desc hash. But the grep/split combo looks only marginally better than the loop example above.

my $data=<<ENDS; [room] some text here, probably something a few lines long [wall] another text here, probably something a few lines long [sometimes with braces] sometimes with a few paragraphs ENDS %desc = grep {$_} (split (/\[(\w+)\]/, $data));

- Luke


In reply to 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.