Since that looks an awful lot like Perl, you might consider just eval'ing it.

Yes, this would allow arbitrary Perl code in your data which may be a bad thing. It could be downright stupid if you don't trust the source of the data. You can mitigate the hazards somewhat by using the Safe module but it is likely that the task would not be worth the extra work. In many cases, however, security isn't an issue and using Perl to parse a Perl-like structure saves a lot of time and reduces code complexity. This thread didn't seem complete without the suggestion. Oops,newrisedesigns did mention it.

#!/usr/bin/perl -w use strict; use Data::Dumper; undef $/; my $text = <DATA>; my $hash = eval '{' . $text . '}'; print Dumper $hash; __DATA__ key0 => 44, "key1" => "fun. \"yes\" 'fun'", 'key2' => 'yahoo\' "fun"'

Check $@ and make use of $SIG{__WARN__} to catch corrupt data.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Regexing a hash line thing by sauoq
in thread Regexing a hash line thing by nutshell

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.