| 1 | package String::ShortenHostname; | ||||||||||
| 2 | # ABSTRACT: tries to shorten hostnames while keeping them meaningful | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION = '0.006'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 1 | has 'length' => ( is => 'rw', isa => 'Int', required => 1 ); | |||||||||
| 10 | 1 | has 'keep_digits_per_domain' => ( is => 'rw', isa => 'Int', default => 5 ); | |||||||||
| 11 | 1 | has 'domain_edge' => ( is => 'rw', isa => 'Maybe[Str]', default => '~' ); | |||||||||
| 12 | 1 | has 'cut_middle' => ( is => 'rw', isa => 'Bool', default => 1 ); | |||||||||
| 13 | |||||||||||
| 14 | 1 | has 'force' => ( is => 'rw', isa => 'Bool', default => 1 ); | |||||||||
| 15 | 1 | has 'force_edge' => ( is => 'rw', isa => 'Maybe[Str]', default => '~>'); | |||||||||
| 16 | |||||||||||
| 17 | 9 | ▶ | sub shorten { | ||||||||
|
Subroutine
shorten: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||
| 18 | 9 | my ( $self, $hostname ) = @_; | |||||||||
| 19 | 9 | my $cur_len = length($hostname); | |||||||||
| 20 | 9 | my ( $host, @domain ) = split('\.', $hostname); | |||||||||
| 21 | |||||||||||
| 22 | 9 | ▶ | if( $cur_len <= $self->length ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | return($hostname); # already short | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 9 | ▶ | for( my $i = scalar(@domain) - 1 ; $i >= 0 ; $i--) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 23 | my $cut_len = $self->keep_digits_per_domain; | |||||||||
| 28 | 23 | my $orig = $domain[$i]; | |||||||||
| 29 | 23 | ▶ | if( length($domain[$i]) <= $self->keep_digits_per_domain ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 9 | next; | |||||||||
| 31 | } | ||||||||||
| 32 | 14 | ▶ | if( defined $self->domain_edge ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 8 | $cut_len -= length($self->domain_edge); | |||||||||
| 34 | } | ||||||||||
| 35 | 14 | ▶ | if( $self->cut_middle ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 5 | $cut_len--; | |||||||||
| 37 | } | ||||||||||
| 38 | 14 | ▶ | if( $cut_len < 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | die('remaining length per domain too small, adjust keep_digits_per_domain, domain_edge, cut_middle'); | |||||||||
| 40 | } | ||||||||||
| 41 | 13 | $domain[$i] = substr($orig, 0, $cut_len); | |||||||||
| 42 | 13 | ▶ | if( defined $self->domain_edge ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 7 | $domain[$i] .= $self->domain_edge; | |||||||||
| 44 | } | ||||||||||
| 45 | 13 | ▶ | if( $self->cut_middle ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 4 | $domain[$i] .= substr($orig, -1, 1); | |||||||||
| 47 | } | ||||||||||
| 48 | 13 | $cur_len = length(join('.', $host, @domain)); | |||||||||
| 49 | |||||||||||
| 50 | 13 | ▶ | if( $cur_len <= $self->length ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 1 | return( join('.', $host, @domain) ); | |||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 7 | $hostname = join('.', $host, @domain); | |||||||||
| 56 | |||||||||||
| 57 | 7 | ▶ | if( $self->force ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 2 | $hostname = substr($hostname, 0, $self->length); | |||||||||
| 59 | 2 | ▶ | if( defined $self->force_edge ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 1 | $hostname = substr($hostname, 0, $self->length - length($self->force_edge)); | |||||||||
| 61 | 1 | $hostname .= $self->force_edge; | |||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 7 | return($hostname); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | 1; | |||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||