File Coverage

blib/lib/Devel/Memalyzer/Combine.pm
Criterion Covered Total %
statement 45 45 100.0
branch 10 16 62.5
condition n/a
subroutine 6 6 100.0
pod 1 2 50.0
total 62 69 89.8


line stmt bran cond sub pod time code
1             package Devel::Memalyzer::Combine;
2 1     1   1044 use strict;
  1         4  
  1         42  
3 1     1   6 use warnings;
  1         1  
  1         38  
4              
5 1     1   15 use IO qw/Handle File Pipe/;
  1         2  
  1         17  
6              
7             our @EXPORT_OK = qw/combine/;
8 1     1   740 use base 'Exporter';
  1         2  
  1         649  
9              
10             sub combine {
11 1     1 1 1731 my ( $dest, %specs ) = @_;
12 1 50       16 die( "No headers found" )
13             unless -e "$dest.head";
14 1 50       11 die( "No data found" )
15             unless -e "$dest.raw";
16              
17 1 50       40 open( my $header_file, '<', "$dest.head" ) || die( "header error: $!" );
18 1 50       25 open( my $data_file, '<', "$dest.raw" ) || die( "data error: $!" );
19 1 50       75 open( my $out_file, '>', $dest ) || die( "output file error: $!" );
20              
21 1         28 my @header_lines = <$header_file>;
22 1         4 chomp( @header_lines );
23 1         3 my @header_sets = map {[split(',', $_ )]} @header_lines;
  4         13  
24 1         5 my @headers = merge_headers( @header_sets );
25              
26 1         15 print $out_file join(',', @headers) . "\n";
27 1         3 my $header_set = shift @header_sets;
28 1         18 while( my $line = <$data_file> ) {
29 15 100       23 if ( $line eq "\n" ) {
30 3         4 $header_set = shift @header_sets;
31 3         13 next;
32             }
33 12         11 chomp( $line );
34 12         26 my @data = split( ',', $line );
35 12         24 my %data_hash = map {( $header_set->[$_] => $data[$_] )} 0 .. (@data - 1);
  43         77  
36 12 100       31 print $out_file join(',', map { $_ || '' } @data_hash{@headers}) . "\n";
  84         220  
37             }
38              
39 1         59 close( $out_file );
40 1         10 close( $header_file );
41 1         10 close( $data_file );
42 1 50       5 unless( $specs{ keep_files }) {
43 1         48 unlink( "$dest.head" );
44 1         42 unlink( "$dest.raw" );
45             }
46             }
47              
48             sub merge_headers {
49 1     1 0 2 my %seen = map {( map {( $_ => 1 )} @$_ )} @_;
  4         11  
  11         16  
50 1         11 return reverse sort keys %seen;
51             }
52              
53             1;
54              
55             __END__