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.002'; # VERSION
5              
6 1     1   18627 use 5.010001;
  1         2  
7 1     1   3 use strict;
  1         1  
  1         18  
8 1     1   3 use warnings;
  1         1  
  1         21  
9              
10 1     1   7 use Exporter qw(import);
  1         1  
  1         228  
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             },
28             },
29             examples => [
30             ],
31             };
32             sub parse_hosts {
33 1     1 1 9 my %args = @_;
34              
35 1         2 my $content = $args{content};
36 1 50       4 unless (defined $content) {
37 0 0       0 open my($fh), "<", "/etc/hosts"
38             or return [500, "Can't read /etc/hosts: $!"];
39 0         0 local $/;
40 0         0 $content = <$fh>;
41             }
42              
43 1         2 my @res;
44 1         6 for my $line (split /^/, $content) {
45 10 100       20 next unless $line =~ /\S/;
46 8         8 chomp $line;
47 8 100       18 next if $line =~ /^\s*#/;
48 7         17 my ($ip, @hosts) = split /\s+/, $line;
49 7         14 push @res, {
50             ip => $ip,
51             hosts => \@hosts,
52             };
53             }
54 1         16 [200, "OK", \@res];
55             }
56              
57             1;
58             # ABSTRACT: Parse /etc/hosts
59              
60             __END__