It's a matter of reliably recognizing keys from values. There are two things that complicate matters:

Now what distinguishes a key from a value, is that it starts at the start of a line, plus some whitespace; it contains only word characters, and is followed by (optional?) whitespace, and a "=" sign. If the whitespace isn't optional, it's easier to prevent false positives for recognized keys, because an URL can't contain whitespace. Still, you can't get an URL containing a "=" character while not containing any other non-word characters on the left of it, in particular any of colons, slashes and question marks.

My preference would go to a solution that involves split. If split gets a regexp that contains capturing parens, the captured values will be inserted between the split data. So I split the values (possibly empty), and capture the keys. You'll get a leading dummy "value", without associated key, so we'll have top get rid of that first.

This is code that works with the two versions of the input, but the regexp might be tweaked to minimize the chance of either false positives, and false negatives.

foreach (<<'-1-', <<'-2-') { <template> foo = Yes bar = 0 blort = home_url = http://www.foo.com/some/really/long/url </template> -1- <template> foo = Yes bar = 0 blort = home_url = http://www.foo.com/some/really/long/url </template> -2- if(m[^<template>(.*?)\s*^</template>]sm) { (undef, my %data) = split /\s*^\s*(\w+)\s+=[^\S\n]*\n?/m, $1, +-1; use Data::Dumper; print Dumper \%data; } else { print "No match\n"; } }
Result:
$VAR1 = { 'blort' => '', 'foo' => 'Yes', 'bar' => '0', 'home_url' => 'http://www.foo.com/some/really/long/url' }; $VAR1 = { 'blort' => '', 'foo' => 'Yes', 'bar' => '0', 'home_url' => 'http://www.foo.com/some/really/long/url' };

In reply to Re: Unwrapping values in a template by bart
in thread Unwrapping values in a template by hacker

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.