Algorithm-CouponCode
view release on metacpan or search on metacpan
lib/Algorithm/CouponCode.pm view on Meta::CPAN
I<Note> the cc_validate function and the Javascript plugin only validate that
the code is 'well-formed' (i.e.: each part has a valid checkdigit). Checking
whether the code is in fact 'valid' is left up to your application and would
typically involve looking for the code in a database. If you use the
Javascript plugin, you might choose to tweak the CSS to keep the red
highlighting of checksum errors but remove the green highlighting which might
imply the code was correct.
=head2 Randomness and Uniqueness
The code returned by C<cc_generate()> is random, but not necessarily unique.
If your application requires unique codes, it is your responsibility to
avoid duplicates (for example by using a unique index on your database column).
The codes are generated using a SHA1 cryptographic hash of a plaintext. If you
do not supply a plaintext, one will be generated for you (using /dev/urandom if
available or Perl's C<rand()> function otherwise). In the event that an
'inappropriate' code is created, the generated hash will be used as a
plaintext input for generating a new hash and the process will be repeated.
Each 4-character part encodes 15 bits of random data, so a 3-part code will
incorporate 45 bits making a total of 2^45 (approximately 35 trillion) unique
codes.
=head1 EXPORTS
The following functions can be exported from the C<Algorithm::CouponCode>
module. No functions are exported by default.
=head2 cc_generate( options )
Returns a coupon code as a string of 4-character parts separated by '-'
characters. The following optional named parameters may be supplied:
=over 4
=item parts
The number of parts desired. Must be a number in the range 1 - 6. Default is
3.
=item plaintext
A byte string which will be hashed using L<Digest::SHA> to produce the code.
If you do not supply your own plaintext then a random one will be generated for
you.
=item bad_regex
You can supply a regular expression for matching 4-letter words which should
not appear in generated output. The C<make_bad_regex()> helper function can
be used to turn a list of words into a suitable regular expression.
=back
=head2 cc_validate( options )
Takes a code, cleans it up and validates the checkdigits. Returns the
normalised (and untainted) version of the code on success or undef on error.
The following named parameters may be supplied:
=over 4
=item code
The code to be validated. The parameter is mandatory.
=item parts
The number of parts you expect the code to contain. Default is 3.
=back
=head2 make_bad_regex( options )
This function is used to compile a list of 4-letter words into a regular
expression suitable for passing to the C<cc_generate()> function. You would
only need to do this if you wished to augment or replace the default list of
undesirable words. The following named parameters may be supplied:
=over 4
=item words
A reference to an array of 4-letter words. The parameter is mandatory.
=item mode
Either the word 'add' or the word 'replace'. Default is 'add'.
=back
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Algorithm::CouponCode
You can also look for information at:
=over 4
=item * Github project issue tracker
L<https://github.com/grantm/Algorithm-CouponCode/issues>
=item * Source Code Respository
L<http://github.com/grantm/Algorithm-CouponCode>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Algorithm::CouponCode>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Algorithm::CouponCode>
( run in 1.743 second using v1.01-cache-2.11-cpan-5b529ec07f3 )