blib/lib/BSD/Getfsent.pm

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