blindluke has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|