blib/lib/BSD/Getfsent.pm

filter: Statementstmt 100% 62 / 62 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 97.7% 88 / 90 SCAR 22.0
CC
9
Cov
97%
_parse_entries
4.0
getfsent
4.0
_open_fh
2.1
CRAP 9.0
↑ Summary Build »
1 package BSD::Getfsent;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our ($VERSION, @EXPORT_OK, $ENTRIES, $FSTAB);
11  
12 3 $VERSION = '0.17';
13 3 @EXPORT_OK = qw(getfsent);
14 3 $ENTRIES = __PACKAGE__ . '::_fsents';
15 3 $FSTAB = '/etc/fstab';
16  
17 sub getfsent
18 {
19 7 ▶ if (wantarray) {
Subroutine getfsent: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 6 1 7
20 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 ▶ unless (${$ENTRIES}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (${$ENTRIES;}) 1 5 6
23 1 @{$ENTRIES} = @{_parse_entries()};
24 1 ${$ENTRIES} = 1;
25 }
26  
27 6 ▶ if (@{$ENTRIES}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ENTRIES;}) { } 5 1 6
28 5 return @{shift @{$ENTRIES}};
29 }
30 else {
31 1 ${$ENTRIES} = 0;
32 1 return ();
33 }
34 }
35 1 else { return _count_entries() }
36 }
37  
38 sub _parse_entries
39 {
40 1 ▶ my @entries;
Subroutine _parse_entries: called CC=4 SCAR=13.9
41 1 my $fh = _open_fh();
42  
43 1 ▶ while (local $_ = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(local $_ = readline $fh)) 5 1 6
44 5 ▶ next if /^\#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\#/ 0 5 5
45  
46 5 chomp;
47 5 my @entry = split;
48  
49 5 ▶ if ($entry[3] !~ /,/) { # In case element 4, fs_type, doesn't
Branch: 1 branch
BranchTrueFalseTotal
if (not $entry[3] =~ /,/) { } 1 4 5
50 1 splice(@entry, 3, 1, '', $entry[3]); # contain fs_mntops, insert blank fs_mntops
51 } # at index 3 and move fs_type to index 4.
52 else { # In case element 4 contains fs_type and
53 4 splice(@entry, 3, 1, # fs_mntops, switch fs_mntops to index 3 and
54 (reverse split ',', $entry[3], 2)); # fs_type to index 4.
55 }
56  
57 5 push @entries, [ @entry ];
58 }
59  
60 1 _close_fh($fh);
61  
62 1 return \@entries;
63 }
64  
65 sub _count_entries
66 {
67 1 ▶ my $counted_entries;
Subroutine _count_entries: called CC=2 SCAR=6.9
68  
69 1 my $fh = _open_fh();
70 1 ▶ $counted_entries++ while <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fh) and do { ++$counted_entries } 5 1 6
71 1 _close_fh($fh);
72  
73 1 return $counted_entries;
74 }
75  
76 sub _open_fh
77 {
78 2 ▶ my $fh = IO::File->new("<$FSTAB")
Subroutine _open_fh: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new("<$FSTAB") or croak("Can't open ${FSTAB}: $!") 0 2 2
79 or croak "Can't open $FSTAB: $!";
80  
81 2 return $fh;
82 }
83  
84 sub _close_fh
85 {
86 2 ▶ my ($fh) = @_;
Subroutine _close_fh: called CC=1 SCAR=0.0
87 2 $fh->close;
88 }
89  
90 3 1;
91 __END__
↑ Summary Build »