Data::ShortNameProvider::Role::Style - Role for Data::ShortNameProvider styles
Define your own style:
package My::DSNP::Style; use Moo; with 'Data::ShortNameProvider::Role::Style'; sub generate_name { my ( $self, $name ) = @_; return "short_$name"; } sub parse_generated_name { my ( $self, $name ) = @_; return if not $self =~ /^short_(.*)$/; return { name => $1 }; } 1;
Use it:
use Data::ShortNameProvider; my $np = Data::ShortNameProvider->new( style => '+My::DSNP::Style' ); my $short_name = $np->generate_shortname($name);
This role provides the basic attributes and requirements for a class to be used as a style provider for Data::ShortNameProvider.
Data::ShortNameProvider actually checks if the provided style class implements this role, and throws an exception if it doesn't.
This is a timestamp in Unix epoch, that may be used by the style to produce short names.
The default is the return value of time().
time()
Data::ShortNameProvider::Role::Style provides a default implementation of some methods. Your style can provide its own version for efficiency reasons.
if( $provider->is_generated_name( $name ) ) { ... }
Return a boolean indicating if the $name string could have been generated by this provider.
$name
my $short_name = $provider->parse_generated_name( $name );
Generate a "short name" for the $name parameter.
my $hash = $provider->parse_generated_name( $short_name );
Return the components of the name as a hash. This method will parse names that might have been generated with a different instance.
$hash should at least contain the name and timestamp_epoch keys. Everything else depends on the style itself, but one should always be able to make a copy of the original provider by passing $hash to the constructor.
$hash
name
timestamp_epoch
my $prev = My::DSNP::Style->new(%args); my $sn = $prev->generate_new_name($name); my $cur = My::DSNP::Style->new(%other_args); my $hash = $cur->parse_generated_name($sn); my $next = My::DSNP::Style->new($hash); # always true $sn eq $next->generate_new_name($name);
Data::ShortNameProvider, Data::ShortNameProvider::Style::Basic.
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.