File Coverage

blib/lib/Parse/Hosts.pm
Criterion Covered Total %
statement 22 25 88.0
branch 5 8 62.5
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 33 39 84.6


line stmt bran cond sub pod time code
1             package Parse::Hosts;
2              
3             our $DATE = '2016-10-17'; # DATE
4             our $VERSION = '0.001'; # VERSION
5              
6 1     1   12927 use 5.010001;
  1         3  
7 1     1   3 use strict;
  1         1  
  1         15  
8 1     1   2 use warnings;
  1         1  
  1         18  
9              
10 1     1   7 use Exporter qw(import);
  1         1  
  1         209  
11             our @EXPORT_OK = qw(parse_hosts);
12              
13             our %SPEC;
14              
15             $SPEC{parse_hosts} = {
16             v => 1.1,
17             summary => 'Parse /etc/hosts',
18             args => {
19             content => {
20             summary => 'Content of /etc/hosts file',
21             description => <<'_',
22              
23             Optional. Will attempt to read `/etc/hosts` from filesystem if not specified.
24              
25             _
26             schema => 'str*',
27             cmdline_src => 'stdin',
28             },
29             },
30             examples => [
31             ],
32             };
33             sub parse_hosts {
34 1     1 1 6 my %args = @_;
35              
36 1         2 my $content = $args{content};
37 1 50       2 unless (defined $content) {
38 0 0       0 open my($fh), "<", "/etc/hosts"
39             or return [500, "Can't read /etc/hosts: $!"];
40 0         0 local $/;
41 0         0 $content = <$fh>;
42             }
43              
44 1         1 my @res;
45 1         4 for my $line (split /^/, $content) {
46 10 100       18 next unless $line =~ /\S/;
47 8         7 chomp $line;
48 8 100       13 next if $line =~ /^\s*#/;
49 7         16 my ($ip, @hosts) = split /\s+/, $line;
50 7         14 push @res, {
51             ip => $ip,
52             hosts => \@hosts,
53             };
54             }
55 1         11 [200, "OK", \@res];
56             }
57              
58             1;
59             # ABSTRACT: Parse /etc/hosts
60              
61             __END__