in reply to Regexing a hash line thing
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.";
|
|---|