Benedikt Bock
Benedikt Bock

Reputation: 1027

Parse Perl Data::Dumper output with ruby

I've a directory full of state files which were printed by Perl's Data::Dumper. The content looks like this:

$VAR1 = {
          'localtime' => 'Tue Jun  6 11:48:20 2017',
          'lookback_history' => {
                                  'ifHCOutOctets' => {
                                                       '1496742350.42394' => '74365529910',
                                                       '1496742455.72943' => '74366309899',
                                                       '1496742446.38562' => '74366309114',
                                                       '1496742500.42388' => '74372744112'
                                                     },
                                  'ifHCInOctets' => {
                                                      '1496742350.42394' => '13198364950',
                                                      '1496742455.72943' => '13198718163',
                                                      '1496742446.38562' => '13198705712',
                                                      '1496742500.42388' => '13199010183'
                                                    }
                                },
          'timestamp' => '1496742500.42388',
          'ifHCOutOctets' => '74372744112',
          'ifHCInOctets' => '13199010183'
        };

I've to analyze if the files contain unreproducible information.

Is there a way in ruby to parse those perl dumps?

Upvotes: 0

Views: 117

Answers (2)

simbabque
simbabque

Reputation: 54333

Since you already have the files and can't change them, one way would be to use a utility that converts them to a format your Ruby understands, like JSON. If you have the JSON module in your Perl distribution (you probably have a system Perl), you also have the json_pp utility.

So you could shell out to that, and let it convert your Perl data structure (Data::Dumper is nothing else than that) to JSON:

$ cat data.pl | json_pp -f eval -t json > data.json

and then use Ruby to convert that JSON to a Ruby data structure:

require 'json'
JSON.parse(File.read('data.json'))
#=> {
#     "localtime" => "Tue Jun  6 11:48:20 2017",
#     "ifHCOutOctets" => "74372744112",
#     "ifHCInOctets" => "13199010183",
#     # ...
#   }

Upvotes: 1

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

If this data is yours and you are positive it contains no unexpected/harmful strings in it, the simplest way would be (assuming the content of the file is what you have posted):

eval(File.read(file))
my_local_var = $VAR1

Upvotes: 4

Related Questions