My script written in Perl parse text output of a command which is structured like this:
Controller information
Controller Status : Optimal
Logical device information
Logical Device number 1
Logical Status : Optimal
Logical Device number 2
Logical Status : Optimal
Parser should fill multidimentional structure:
{
"controller": {
"Controller Status": "Optimal"
}
"logical": [
{
"Logical Status": "Optimal"
},
{
"Logical Status": "Optimal"
}
]
}
I try parsing using dynamic variables:
foreach (@lines) {
$variable = "info{controller}" if (m/Controller information/);
$variable = "info{logical}[$1]" if (m/Logical Device number (\d+)/);
${$variable}{1} = $2 if (m/\s+(.*?)\s+:\s(.*)$/);
}
In this case header will set hash key corresponding to the topic and all following parameters will be put into chosen key.
The first problem is that dynamic ${$variable} will not work if $variable contain any hash or array keys. Is there a way to make dynamic variables work with hash keys inside?
The second problem is that dynamic variables are called "always-a-bad-idea", and I wonder if there is short but effective way to build parsing function without dynamic variables?