Deadman Deadman -4 years ago 111
JSON Question

Perl - interpolate JSON string values

I have a json file like this:

"tool_name": {
"command": "$ENV{TOOL_BIN_DIR}/some_file_name",
"args": "some args"

I am using
from Perl 5.14. and using
function to read the file and get data into perl hash.

But when I refer to this read data from code like this:

my $cmd = "$data->{tool_name}->{command}";
print $cmd;

I get


How can I make perl resolve the value of this variable?

This example uses enviornment variable but in general if I want to use variables from JSON - how can I do that?

Answer Source

You can use eval like in:

my $cmd = '$ENV{HOME}/toto';
print eval('"' . $cmd . '"'), "\n";

But remember, this is very unsafe from a security point of view. You should probably avoid having to do this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download