2

I apologize if my title is confusing. I'll try to explain.

I'm taking over for someone who left my company abruptly and have little experience with Perl programming. My O'Reilly books, Google, and searches here have led me into an increasingly confusing spiral and I'm at a dead end.

I have an auto-generated hash which is periodically updated and saved on a server as a text file. The file contents are as such:

$VAR1 = {
   'qa_metrics' => {
      'group_level' => {
      },
      'product_level' => {
         'qa_metric_1' => [ 
            {
            'operator' => '>',
            'limit' => '5',
            }
         ],
         'qa_metric_2' => [ 
            {
            'operator' => '>',
            'limit' => '5',
            }
         ]
      }
   }
   'speed_metrics' => {
      'group_level' => {
         'group_speed_metric_1' => [
            {
            'operator' => '>=',
            'limit' => '6',
            }
         ]
      },
      'product_level' => {
         'speed_metric_1' => [
            {
            'operator' => '>=',
            'limit' => '6',
            }
         ],
          'speed_metric_2' => [
            {
            'operator' => '<=',
            'limit' => '13',
            }
         ]
      }
   }
   'other_metrics' => {
      'product_level' => {
         'other_metric_1' => [
            {
            'operator' => '==',
            'limit' => '1',
            }
         ],
         'other_metric_2' => [
            {
            'operator' => '<',
            'limit' => '3',
            }
         ]
      }
   }
}

The project I'm working on requires that I walk through the multi-level hash structure and push all of the 'product_level' metric names into an array based on metric type. For example, qa_metric_1, qa_metric_2, and any others of that type (qa) and level (product) would be pushed into one array while speed_metric_1, speed_metric_2, and other_metric_1, other_metric_2, etc would be pushed into their own separate arrays.

Once the arrays are populated and contained in ,, and ``, or similar, I believe I can proceed from that point on my own.

I should mention that I am working with Perl 5.8 and my ability (meaning permission from IT) to add 3rd-party modules is almost nonexistent.

Any help would be much appreciated.

G. Cito
  • 6,210
  • 3
  • 29
  • 42
arichens
  • 23
  • 2
  • A question should provide what you have tried and where the problem is, not 'do the whole for me' for free – Gilles Quénot Dec 16 '14 at 16:25
  • Answering is difficult, because it looks like you're trying to pars e the output of `Data::Dumper` - something that's a non trivial operation. You'd generally be better to output the data further upstream e.g. using `Storable` instead, because that's designed to be read back in again. – Sobrique Dec 16 '14 at 16:27
  • If you really want to dump your data-structures and read them back in (or `eval`) or recreate from text files you need to tell `Data::Dumper` to "purify" the output by setting `$Data::Dumper::Purity = 1;` in your script. – G. Cito Dec 16 '14 at 18:14
  • If you've got control of the source script, I'd suggest the answer is to not use `Data::Dumper` in the first place. – Sobrique Dec 16 '14 at 18:44

1 Answers1

2

This looks like what you'd get if you used the Data::Dumper module, and just printed it to a file. I think that'd be a bit of an unusual thing to do if you wanted to read it back in again. It's a debug tool, and there'd be better ways of extracting information in a re-readable format.

I'd suggest the place to look is where that output comes out in the first place, and use Storable (core module, I think it'll be in 5.8) to save it to a file that you can easily re-read.

Failing that though there's a few hacks that can be applied to do this. But don't mistake them for being 'good solutions' because the 'good solution' is to not do this in the first place!

For example - this should work, but is EXCEEDINGLY dangerous if you don't have control over the file contents. (This is basically 'run some code in this file, and hope it doesn't do anything horrible' sort of approach. That's generally bad news, and to be avoided).

See: How do I read back in the output of Data::Dumper?

use strict;
use warnings;

our $VAR1;

open ( my $input_fh, "<", "your_file.txt" );
{
    local $/;     
    $VAR1 = eval <$input_fh>;
    warn $@ if $@;
}

foreach my $key ( keys %{$VAR1} ) {
    print "$key:\n";
 }

That's with a caveat - is there any chance there's a typo in that data you posted, and you omitted some commas? Otherwise it's not a valid data structure. (you need a comma between each 'set' qa_metrics, speed_metrics etc.).

Once you've done that, you've got a hash that you can then manipulate in a way you feel is appropriate - traversing the hash to extract the data you want should be fairly straightforward.

Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101