| 1 | package String::Ident; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = 0.05; | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Text::Unidecode 'unidecode'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 9 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 13 | 9 | my ( $class, %args ) = @_; | |||||||||
| 14 | return bless { | ||||||||||
| 15 | min_len => $args{min_len}, | ||||||||||
| 16 | max_len => $args{max_len}, | ||||||||||
| 17 | 9 | }, $class; | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | 18 | ▶ | sub min_len { | ||||||||
|
Subroutine
min_len: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 21 | 18 | my ( $self, $new ) = @_; | |||||||||
| 22 | 18 | ▶ | $self->{min_len} = $new if @_ > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | $self->{min_len} = 4 | ||||||||||
| 24 | 18 | ▶ | unless defined( $self->{min_len} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 18 | return $self->{min_len}; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 8 | ▶ | sub max_len { | ||||||||
|
Subroutine
max_len: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 29 | 8 | my ( $self, $new ) = @_; | |||||||||
| 30 | 8 | ▶ | $self->{max_len} = $new if @_ > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | $self->{max_len} = 30 | ||||||||||
| 32 | 8 | ▶ | unless defined( $self->{max_len} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 8 | return $self->{max_len}; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 12 | ▶ | sub cleanup { | ||||||||
|
Subroutine
cleanup: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 37 | 12 | my ( $self, $text, $max_len ) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 12 | ▶ | $self = $self->new( max_len => $max_len ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | unless blessed($self); | ||||||||||
| 41 | 12 | ▶ | $max_len = ( defined($max_len) ? $max_len : $self->max_len ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 12 | ▶ | $text = '' unless defined($text); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 12 | $text = unidecode($text); | |||||||||
| 45 | 12 | $text =~ s/[^-A-Za-z0-9]/-/g; | |||||||||
| 46 | 12 | $text =~ s/--+/-/g; | |||||||||
| 47 | 12 | $text =~ s/-$//g; | |||||||||
| 48 | 12 | $text =~ s/^-//g; | |||||||||
| 49 | |||||||||||
| 50 | 12 | ▶ | if ( $max_len > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 8 | $text = substr( $text, 0, $max_len ); | |||||||||
| 52 | } | ||||||||||
| 53 | 12 | ▶ | while ( length($text) < $self->min_len ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 6 | $text .= chr( ord('a') + rand( ord('z') - ord('a') + 1 ) ); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 12 | return $text; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | 1; | |||||||||
| 61 | |||||||||||
| 62 | __END__ | ||||||||||