blib/lib/String/ShortHostname.pm

filter: Statementstmt 100% 37 / 37 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 93.4% 57 / 61 CC 8 SCAR 10.5
CC
8
Cov
96%
short_hostname
5.0
__ANON__
3.0
__ANON__
2.0
CRAP 2.9
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package String::ShortHostname;
4  
5 # ABSTRACT: extracts the first field from an FQDN
6  
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 1 Moose::Exporter->setup_import_methods( as_is => [ 'short_hostname' ]);
13  
14 1 has 'hostname' => (
15 is => 'rw',
16 isa => 'Str',
17 required => 1,
18 default => '',
19 predicate => 'has_hostname',
20 );
21  
22 2 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
23 2 my $orig = shift;
24 2 my $class = shift;
25  
26 2 ▶ if ( @_ == 1 && !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 1 1 2
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
101
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
27 1 return $class->$orig( hostname => short_hostname($_[0]) );
28 }
29 else {
30 1 return $class->$orig(@_);
31 }
32 1 };
33  
34 3 ▶around 'hostname' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
35 3 my $orig = shift;
36 3 my $self = shift;
37  
38 3 ▶ return $self->$orig() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 1 3
39  
40 1 return $self->$orig( short_hostname( shift ) );
41 1 };
42  
43 4 ▶sub short_hostname {
Subroutine short_hostname: called CC=5 SCAR=16.1
Pod undocumented
44 4 my $hostname = shift;
45 4 my @bits;
46 4 ▶ @bits = split /\./, $hostname if $hostname;
Branch: 1 branch
BranchTrueFalseTotal
if $hostname 4 0 4
47 4 my $alpha_found;
48 4 for( @bits ){
49 13 ▶ $alpha_found = 1 if /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if /\D/ 9 4 13
50 }
51 4 ▶ if( $alpha_found ){
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha_found) { } 3 1 4
52 3 return $bits[0];
53 } else {
54 1 return $hostname; # probably an IP Address
55 }
56 }
57  
58  
59  
60 1 1;
61  
62 __END__
↑ Summary