Data::ShortNameProvider - Generate short names with style
Create a name provider:
my $np = Data::ShortNameProvider->new( style => 'Basic', # default timestamp_epoch => 1400023019, # defaults to time() max_name_length => 32, # croak if a longer name is generated # style-specific arguments prefix => 'dbit', version => 1, );
Generate a shortname:
$short_name = $np->generate_name('foo'); # returns "dbit1_140513__foo"
Parse a generated shortname:
$hash = $np->parse_generated_name($short_name);
$hash contains something like:
$hash
# depends on the style { prefix => 'dbit', version => 1, timestamp => '140513', timestamp_epoch => 1400023019, name => 'foo', }
or undef if $short_name could not be parsed as a short name generated with that style.
undef
$short_name
Check if a string is parsable:
my @names = grep { $np->is_generated_name($_) } @names;
Create short names that encode a timestamp and a fixed label in a format that's unlikely to match normal names.
A typical use-case would be the creation of database table names or file names in situations where you need to minimize the risk of clashing with existing items.
The generated names can be detected and parsed to extract the timestamp and other components.
The fully-qualified name of the style class that actually generates the short names.
If the constructor argument does not contain the :: package separator, the style name is considered to be a short-cut and will be prefixed with Data::ShortNameProvider::Style:: to produce the fully-qualified style class name.
::
Data::ShortNameProvider::Style::
A maximum length constraint on the generated short names.
"generate_name" will die if the "short name" returned by the style is longer than max_name_length.
max_name_length
Setting max_name_length to 0 removes this constraint.
0
The instance of "style" to which most of the actual work is delegated.
my $short_name = $dsnp->generate_name($name);
Delegated to the "provider" object, but enforces some additional restrictions ("max_name_length").
my $hash = $dsnp->parse_generated_name($short_name);
Parses a name that was generated by the "provider" class, and returns its constituents as a hash reference.
Delegates to the "provider" object, and add the "style" and "max_name_length" keys, so that one is always be able to make a copy of the object by doing:
my $clone = Data::ShortNameProvider->new($hash);
if( $dsnp->is_generated_name( $name ) ) { ... }
Return a boolean indicating if the $name string could have been generated by this provider.
$name
Delegated to the "provider" object.
This is a read-write accessor to the provider's timestamp_epoch attribute.
This module is based on an idea and proposal by Tim Bunce, on the dbi-dev mailing-list.
dbi-dev
The initial thread about Test::Database shortcomings: http://www.nntp.perl.org/group/perl.dbi.dev/2014/04/msg7792.html
Tim's proposal for a short name provider: http://www.nntp.perl.org/group/perl.dbi.dev/2014/05/msg7815.html
The first implementaion of the module was written during the first two days of the Perl QA Hackathon 2015 in Berlin (with Tim Bunce providing extensive feedback on IRC). Many thanks to TINITA for organizing this event!
Philippe Bruhat (BooK), <book@cpan.org>.
Copyright 2014-2015 Philippe Bruhat (BooK), all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Data::ShortNameProvider, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::ShortNameProvider
CPAN shell
perl -MCPAN -e shell install Data::ShortNameProvider
For more information on module installation, please visit the detailed CPAN module installation guide.