Business::BR::Ids - Modules for dealing with Brazilian identification codes (CPF, CNPJ, ...)


Business-BR-Ids documentation Contained in the Business-BR-Ids distribution.

Index


Code Index:

NAME

Top

Business::BR::Ids - Modules for dealing with Brazilian identification codes (CPF, CNPJ, ...)

SYNOPSIS

Top

  use Business::BR::Ids;
  my $cpf = '390.533.447-05';
  print "ok as CPF" if test_id('cpf', $cpf);
  my $cnpj = '90.117.749/7654-80';
  print "ok as CNPJ" if test_id('cnpj', $cnpj);

DESCRIPTION

Top

This is a generic module for handling the various supported operations on Brazilian identification numbers and codes. For example, it is capable to test the correctness of CPF, CNPJ and IE numbers without the need for explicitly 'requiring' or 'using' this modules (doing it automatically on demand).

test_id
  test_id($entity_type, @args); 
  test_id('cpf', $cpf); # the same as "require Business::BR::CPF; Business::BR::CPF::test_cpf($cpf)"

Tests for correct inputs of ids which have a corresponding Business::BR module. For now, the supported id types are 'cpf', 'cnpj', 'ie', and 'pis'.

canon_id
  canon_id($entity_type, @args)

Transform the input to a canonical form. The canonical form is well-defined and as short as possible. For instance, canon_id('cpf', '29.128.129-11') returns '02912812911' which has exactly 11 digits and no extra character.

EXPORT

test_id is exported by default. canon_id, format_id, parse_id and random_id are exported on demand.

SEE ALSO

Top

Details on handling CPF, CNPJ, IE and PIS can be found in the specific modules:

Please reports bugs via CPAN RT, http://rt.cpan.org/NoAuth/Bugs.html?Dist=Business-BR-Ids

AUTHOR

Top

A. R. Ferreira, <ferreira@cpan.org>

COPYRIGHT AND LICENSE

Top


Business-BR-Ids documentation Contained in the Business-BR-Ids distribution.

package Business::BR::Ids;

use 5;
use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw( canon_id parse_id format_id random_id );
our @EXPORT = qw( test_id );

our $VERSION = '0.0022';
$VERSION = eval $VERSION;

use Carp;

# a hash from entity types to packages
my %types = (
  cpf => 'Business::BR::CPF',
  cnpj => 'Business::BR::CNPJ',
  ie => 'Business::BR::IE',
  pis => 'Business::BR::PIS',
);


# invoke($type, $subroot, @args)
sub _invoke {
  my $type = lc shift;
  my $subroot = shift;
  my $package = $types{$type}
    or croak "unknown '$type'\n";
  eval "require $package";
  croak $@ if $@;
  no strict 'refs';
  return &{"${package}::${subroot}${type}"}(@_);
}

sub test_id {
  return _invoke(shift, 'test_', @_);
}

sub canon_id {
  return _invoke(shift, 'canon_', @_);
}

sub format_id {
  return _invoke(shift, 'format_', @_);
}

sub parse_id {
  return _invoke(shift, 'parse_', @_);
}

sub random_id {
  return _invoke(shift, 'random_', @_);
}

1;

__END__