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