| 1 | #!/usr/bin/perl | ||||||||||
| 2 | #------------------------------------------------------------------------------- | ||||||||||
| 3 | # Generate a unique but stable name from a string | ||||||||||
| 4 | # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2016 | ||||||||||
| 5 | #------------------------------------------------------------------------------- | ||||||||||
| 6 | # podDocumentation | ||||||||||
| 7 | |||||||||||
| 8 | package Data::Unique::Name; | ||||||||||
| 9 | 1 | require v5.16.0; | |||||||||
| 10 | 1 | ▶ | use warnings FATAL => qw(all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | our $VERSION = '20170810'; | |||||||||
| 15 | |||||||||||
| 16 | #1 Constructor | ||||||||||
| 17 | sub new($) # Construct a new set of unique names | ||||||||||
| 18 | 1 | ▶ | {my ($length) = @_; # Maximum length of generated names | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 19 | 1 | bless {length=>$length, count=>{}} | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | #1 Methods | ||||||||||
| 23 | sub generateUniqueName($$) # Generate a unique name corresponding to a string | ||||||||||
| 24 | 10 | ▶ | {my ($set, $string) = @_; # Set of unique strings, string | ||||||||
|
Subroutine
generateUniqueName: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 25 | 10 | my $l = $set->{length}; | |||||||||
| 26 | 10 | my $s = $string =~ s/\W//gsr =~ s/\d+\Z//sr; | |||||||||
| 27 | 10 | ▶ | $s = substr($s, 0, $l) if length($s) > $l; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 10 | ▶ | if (my $n = $set->{count}{$s}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 8 | {$set->{count}{$s}++; | |||||||||
| 30 | 8 | return $s.$n; | |||||||||
| 31 | } | ||||||||||
| 32 | 2 | $set->{count}{$s}++; | |||||||||
| 33 | 2 | $s | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | # podDocumentation | ||||||||||
| 37 | |||||||||||
| 38 | #------------------------------------------------------------------------------- | ||||||||||
| 39 | # Export | ||||||||||
| 40 | #------------------------------------------------------------------------------- | ||||||||||
| 41 | |||||||||||
| 42 | 1 | require Exporter; | |||||||||
| 43 | |||||||||||
| 44 | 1 | ▶ | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | 1 | @ISA = qw(Exporter); | |||||||||
| 47 | 1 | @EXPORT = qw(); | |||||||||
| 48 | 1 | @EXPORT_OK = qw(); | |||||||||
| 49 | 1 | %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]); | |||||||||
| 50 | |||||||||||
| 51 | =encoding utf-8 | ||||||||||
| 52 | |||||||||||
| 53 | =head1 Name | ||||||||||
| 54 | |||||||||||
| 55 | Data::Unique::Name - Generate a unique but stable name from a string | ||||||||||
| 56 | |||||||||||
| 57 | =head1 Synopsis | ||||||||||
| 58 | |||||||||||
| 59 | use Data::Unique::Name; | ||||||||||
| 60 | |||||||||||
| 61 | my $u = Data::Unique::Name::new(4); | ||||||||||
| 62 | |||||||||||
| 63 | ok $u->generateUniqueName("aaaa") eq "aaaa"; | ||||||||||
| 64 | ok $u->generateUniqueName("aaaa") eq "aaaa1"; | ||||||||||
| 65 | ok $u->generateUniqueName("aaaa1") eq "aaaa2"; | ||||||||||
| 66 | ok $u->generateUniqueName("aaaa2") eq "aaaa3"; | ||||||||||
| 67 | ok $u->generateUniqueName("aaaab") eq "aaaa4"; | ||||||||||
| 68 | ok $u->generateUniqueName("a a a a b") eq "aaaa5"; | ||||||||||
| 69 | ok $u->generateUniqueName("a-a(a)/ab") eq "aaaa6"; | ||||||||||
| 70 | ok $u->generateUniqueName("bbbbb") eq "bbbb"; | ||||||||||
| 71 | ok $u->generateUniqueName("bbbbbb") eq "bbbb1"; | ||||||||||
| 72 | ok $u->generateUniqueName("bbbbbbb") eq "bbbb2"; | ||||||||||
| 73 | |||||||||||
| 74 | =head1 Description | ||||||||||
| 75 | |||||||||||
| 76 | =head2 Constructor | ||||||||||
| 77 | |||||||||||
| 78 | =head3 new | ||||||||||
| 79 | |||||||||||
| 80 | Construct a new set of unique names | ||||||||||
| 81 | |||||||||||
| 82 | 1 $length Maximum length of generated names # File name to be used on S3 | ||||||||||
| 83 | |||||||||||
| 84 | =head2 Methods | ||||||||||
| 85 | |||||||||||
| 86 | =head3 generateUniqueName | ||||||||||
| 87 | |||||||||||
| 88 | Generate a unique name corresponding to a string | ||||||||||
| 89 | |||||||||||
| 90 | 1 $set Set of unique strings | ||||||||||
| 91 | 2 $string String | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | =head1 Index | ||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | L<generateUniqueName|/generateUniqueName> | ||||||||||
| 98 | |||||||||||
| 99 | L<new|/new> | ||||||||||
| 100 | |||||||||||
| 101 | =head1 Installation | ||||||||||
| 102 | |||||||||||
| 103 | This module is written in 100% Pure Perl and, thus, it is easy to read, use, | ||||||||||
| 104 | modify and install. | ||||||||||
| 105 | |||||||||||
| 106 | Standard Module::Build process for building and installing modules: | ||||||||||
| 107 | |||||||||||
| 108 | perl Build.PL | ||||||||||
| 109 | ./Build | ||||||||||
| 110 | ./Build test | ||||||||||
| 111 | ./Build install | ||||||||||
| 112 | |||||||||||
| 113 | =head1 Author | ||||||||||
| 114 | |||||||||||
| 115 | L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com> | ||||||||||
| 116 | |||||||||||
| 117 | L<http://www.appaapps.com|http://www.appaapps.com> | ||||||||||
| 118 | |||||||||||
| 119 | =head1 Copyright | ||||||||||
| 120 | |||||||||||
| 121 | Copyright (c) 2016-2017 Philip R Brenan. | ||||||||||
| 122 | |||||||||||
| 123 | This module is free software. It may be used, redistributed and/or modified | ||||||||||
| 124 | under the same terms as Perl itself. | ||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | # Tests and documentation | ||||||||||
| 130 | |||||||||||
| 131 | sub test | ||||||||||
| 132 | 1 | ▶ | {my $p = __PACKAGE__; | ||||||||
|
Subroutine
test: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||
| 133 | 1 | ▶ | return if eval "eof(${p}::DATA)"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 1 | my $s = eval "join('', <${p}::DATA>)"; | |||||||||
| 135 | 1 | ▶ | $@ and die $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 1 | ▶ | eval $s; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 137 | 1 | ▶ | $@ and die $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 1 | ▶ | test unless caller; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | |||||||||||
| 142 | 1 | 1; | |||||||||
| 143 | __DATA__ | ||||||||||