You can also build a parser with
TheDamian's
Regexp::Grammars.
Here is a proof of concept, which you can improve.
use Regexp::Grammars;
my $parser =
qr{
(?:
<[Hash]> ** ( , )
)
<token: Hash>
\{
<[KV]> ** ( , )
\}
<token: KV>
(?:
\s*
<[Key]>
\s*
=>
\s*
<[Value]>
\s*
)
<token: Key>
\w*
<token: Value>
'
\w*
'
}x;
use Data::Dumper;
# Match the grammar against some text...
if ("{ foo => 'bar', baz => 'bof' }, {bing => 'go'}" =~ $parser) {
# If successful, the hash %/ will have the hierarchy of results..
+.
print Dumper(\%/ );
}
__END__
$VAR1 = {
'' => '{ foo => \'bar\', baz => \'bof\' }, {bing => \'go\'}'
+,
'Hash' => [
{
'' => '{ foo => \'bar\', baz => \'bof\' }',
'KV' => [
{
'' => ' foo => \'bar\'',
'Value' => [
'\'bar\''
],
'Key' => [
'foo'
]
},
{
'' => 'baz => \'bof\' ',
'Value' => [
'\'bof\''
],
'Key' => [
'baz'
]
}
]
},
{
'' => '{bing => \'go\'}',
'KV' => [
{
'' => 'bing => \'go\'',
'Value' => [
'\'go\''
],
'Key' => [
'bing'
]
}
]
}
]
};
print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});