File Coverage

blib/lib/Makefile/Update/Bakefile0.pm
Criterion Covered Total %
statement 36 37 97.3
branch 13 14 92.8
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 1 100.0
total 56 59 94.9


line stmt bran cond sub pod time code
1             package Makefile::Update::Bakefile0;
2             # ABSTRACT: Update bakefile-0.x files list.
3              
4 1     1   33267 use Exporter qw(import);
  1         2  
  1         44  
5             our @EXPORT = qw(update_bakefile_0);
6              
7 1     1   4 use strict;
  1         1  
  1         24  
8 1     1   3 use warnings;
  1         1  
  1         305  
9              
10             our $VERSION = '0.2'; # VERSION
11              
12              
13              
14             sub update_bakefile_0
15             {
16 1     1 1 3075 my ($in, $out, $vars) = @_;
17              
18             # Variable whose contents is being currently replaced.
19 1         1 my $var;
20              
21             # Hash with files defined for the specified variable as keys and 0 or 1
22             # depending on whether we have seen them in the input file as values.
23             my %files;
24              
25             # Set to 1 if we made any changes.
26 1         2 my $changed = 0;
27 1         7 while (<$in>) {
28 21         16 chomp;
29              
30 21 100 66     63 if (// && exists $vars->{$1}) {
    100          
31 2         2 $var = $1;
32 2         3 %files = map { $_ => 0 } @{$vars->{$var}};
  7         14  
  2         4  
33             } elsif (defined $var) {
34 9         10 local $_ = $_;
35 9         12 s///;
36 9         13 s/^\s+//;
37 9         10 s/\s+$//;
38 9 100       15 if (m{}) {
    100          
39             # Check if we have any new files.
40             #
41             # TODO Insert them in alphabetical order.
42 2         7 while (my ($file, $seen) = each(%files)) {
43 7 100       16 if (!$seen) {
44             # This file was wasn't present in the input, add it.
45             # TODO Use proper indentation.
46 2         3 print $out " $file\n";
47              
48 2         4 $changed = 1;
49             }
50             }
51              
52 2         2 undef $var;
53             } elsif ($_) {
54 6 100       12 if (not exists $files{$_}) {
55             # This file was removed.
56 1         1 $changed = 1;
57 1         3 next;
58             }
59              
60 5 50       7 if ($files{$_}) {
61 0         0 warn qq{Duplicate file "$_" in the definition of the } .
62             qq{variable "$var" at line $.\n}
63             } else {
64 5         6 $files{$_} = 1;
65             }
66             }
67             }
68              
69 20         50 print $out "$_\n";
70             }
71              
72             $changed
73 1         3 }
74              
75             __END__