File Coverage

blib/lib/Makefile/Update/Bakefile0.pm
Criterion Covered Total %
statement 38 39 97.4
branch 13 14 92.8
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 1 100.0
total 58 61 95.0


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   88234 use Exporter qw(import);
  1         11  
  1         38  
5             our @EXPORT = qw(update_bakefile_0);
6              
7 1     1   5 use strict;
  1         1  
  1         15  
8 1     1   11 use warnings;
  1         2  
  1         349  
9              
10             our $VERSION = '0.4'; # VERSION
11              
12              
13              
14             sub update_bakefile_0
15             {
16 1     1 1 2887 my ($in, $out, $vars) = @_;
17              
18             # Variable whose contents is being currently replaced.
19 1         3 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         1 my $changed = 0;
27 1         6 while (<$in>) {
28 21         30 chomp;
29              
30 21 100 66     56 if (// && exists $vars->{$1}) {
    100          
31 2         4 $var = $1;
32 2         4 %files = map { $_ => 0 } @{$vars->{$var}};
  7         15  
  2         3  
33             } elsif (defined $var) {
34 9         11 local $_ = $_;
35 9         14 s///;
36 9         19 s/^\s+//;
37 9         16 s/\s+$//;
38 9         10 s{]+>}{};
39 9         10 s{}{};
40 9 100       19 if (m{}) {
    100          
41             # Check if we have any new files.
42             #
43             # TODO Insert them in alphabetical order.
44 2         7 while (my ($file, $seen) = each(%files)) {
45 7 100       16 if (!$seen) {
46             # This file was wasn't present in the input, add it.
47             # TODO Use proper indentation.
48 2         5 print $out " $file\n";
49              
50 2         4 $changed = 1;
51             }
52             }
53              
54 2         4 undef $var;
55             } elsif ($_) {
56 6 100       11 if (not exists $files{$_}) {
57             # This file was removed.
58 1         10 $changed = 1;
59 1         3 next;
60             }
61              
62 5 50       9 if ($files{$_}) {
63 0         0 warn qq{Duplicate file "$_" in the definition of the } .
64             qq{variable "$var" at line $.\n}
65             } else {
66 5         7 $files{$_} = 1;
67             }
68             }
69             }
70              
71 20         69 print $out "$_\n";
72             }
73              
74             $changed
75 1         5 }
76              
77             __END__