Assuming (a) the modification proposed by Eliya, and (b) that you placed correctly a comma at the correct places, you end up with a string $s containing the whole content of the file, modified like this:

" KEY1 => 'VALUE1', \n KEY2 => [ 'VALUE21',\n 'VALUE22'\n'VALUE23' + ]"
If you manage to come so far, you can easily get a reference to this hash with
my $hashref=eval "{${s}}"; if($@) { print STDERR "Error in input: $@\n"; } else { # Dump the data just read print("Key $_ has value ",$hashref->{$_},"\n") for keys %$s; }

So perhaps the most tricky part is to get the commas in the right place. To do this, I would first place a comma in front of every /\b(\S+?\s*=>)/, which leaves us one extra comma near the start of the string, which you have to remove afterwards. Well, maybe there is also a simpler solution to this. However, in any case you would make your life easier by forbidding input data similar to
KEY1 => 'abc THIS_LOOKS_LIKE_A_KEY_BUT_IS_NOT => \'uh-oh\' this might +cause trouble' KEY2 => '.....'
-- 
Ronald Fischer <ynnor@mm.st>

In reply to Re^3: Initialize an hash with string by rovf
in thread Initialize an hash with string by Anonymous Monk

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.