.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Business::BR::CPF 3pm" .TH Business::BR::CPF 3pm "2022-10-13" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Business::BR::CPF \- Perl module to test for correct CPF numbers .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Business::BR::CPF; \& \& print "ok " if test_cpf(\*(Aq390.533.447\-05\*(Aq); # prints \*(Aqok \*(Aq \& print "bad " unless test_cpf(\*(Aq231.002.999\-00\*(Aq); # prints \*(Aqbad \*(Aq .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The \s-1CPF\s0 number is an identification number of Brazilian citizens emitted by the Brazilian Ministry of Revenue, which is called \&\*(L"Ministério da Fazenda\*(R". .PP \&\s-1CPF\s0 stands for \*(L"Cadastro de Pessoa Física\*(R" (literally, physical person registration) as opposed to the \s-1CNPJ\s0 number for companies. .PP The \s-1CPF\s0 is comprised of a base of 9 digits and 2 check digits. It is usually written like '231.002.999\-00' so as to be more human-readable. .PP This module provides \f(CW\*(C`test_cpf\*(C'\fR for checking that a \s-1CPF\s0 number is \fIcorrect\fR. Here a \fIcorrect \s-1CPF\s0 number\fR means .IP "\(bu" 4 it is 11 digits long .IP "\(bu" 4 it satisfies the two check equations mentioned below .PP Before checking, any non-digit letter is stripped, making it easy to test formatted entries like '231.002.999\-00' and entries with extra blanks like ' 999.221.222\-00 '. .IP "\fBtest_cpf\fR" 4 .IX Item "test_cpf" .Vb 3 \& test_cpf(\*(Aq999.444.333\-55\*(Aq) # incorrect CPF, returns 0 \& test_cpf(\*(Aq 263.946.533\-30 \*(Aq) # is ok, returns 1 \& test_cpf(\*(Aq888\*(Aq) # nope, returns undef .Ve .Sp Tests whether a \s-1CPF\s0 number is correct. Before testing, any non-digit character is stripped. Then it is expected to be 11 digits long and to satisfy two check equations which validate the last two check digits. See \*(L"\s-1THE CHECK EQUATIONS\*(R"\s0. .Sp The policy to get rid of '.' and '\-' is very liberal. It indeeds discards anything that is not a digit (0, 1, ..., 9) or letter. That is handy for discarding spaces as well .Sp .Vb 1 \& test_cpf(\*(Aq 263.946.533\-30 \*(Aq) # is ok, returns 1 .Ve .Sp But extraneous inputs like '#333%444*2.3+2\-00' are also accepted. If you are worried about this kind of input, just check against a regex: .Sp .Vb 2 \& warn "bad CPF: only digits (11) expected" \& unless ($cpf =~ /^\ed{11}$/); \& \& warn "bad CPF: does not match mask \*(Aq_\|_\|_._\|_\|_._\|_\|_\-_\|_\*(Aq" \& unless ($cpf =~ /^\ed{3}\e.\ed{3}\e.\ed{3}\-\ed{2}$/); .Ve .Sp \&\s-1NOTE.\s0 Integer numbers like 9999811299 (or 99_998_112_99) with fewer than 11 digits will be normalized (eg. to \&\*(L"09999811299\*(R") before testing. .IP "\fBcanon_cpf\fR" 4 .IX Item "canon_cpf" .Vb 2 \& canon_cpf(99); # returns \*(Aq00000000099\*(Aq \& canon_cpf(\*(Aq999.999.999\-99\*(Aq); # returns \*(Aq99999999999\*(Aq .Ve .Sp Brings a candidate for a \s-1CPF\s0 number to a canonical form. In case, the argument is an integer, it is formatted to at least eleven digits. Otherwise, it is stripped of any non-alphanumeric characters and returned as it is. .IP "\fBformat_cpf\fR" 4 .IX Item "format_cpf" .Vb 1 \& format_cpf(\*(Aq00000000000\*(Aq); # returns \*(Aq000.000.000\-00\*(Aq .Ve .Sp Formats its input into '000.000.000\-00' mask. First, the argument is canon'ed and then dots and hyphen are added to the first 11 digits of the result. .IP "\fBparse_cpf\fR" 4 .IX Item "parse_cpf" .Vb 2 \& ($base, $dv) = parse_cpf($cpf); \& $hashref = parse_cpf(\*(Aq999.222.111\-00\*(Aq); # { base => \*(Aq999222111\*(Aq, dv => \*(Aq00\*(Aq } .Ve .Sp Splits a candidate for \s-1CPF\s0 number into base and check digits (dv \- dígitos de verificação). It canon's the argument before splitting it into 9\- and 2\-digits parts. In a list context, returns a two-element list with the base and the check digits. In a scalar context, returns a hash ref with keys 'base' and 'dv' and associated values. .IP "\fBrandom_cpf\fR" 4 .IX Item "random_cpf" .Vb 1 \& $rand_cpf = random_cpf($valid); \& \& $correct_cpf = random_cpf(); \& $cpf = random_cpf(1); # also a correct CPF \& $bad_cpf = random_cpf(0); # an incorrect CPF .Ve .Sp Generates a random \s-1CPF.\s0 If \f(CW$valid\fR is omitted or 1, it is guaranteed to be \fIcorrect\fR. If \f(CW$valid\fR is 0, it is guaranteed to be \fIincorrect\fR. This function is intented for mass test. (Use it wisely.) .Sp The implementation is simple: just generate a 9\-digits random number, hopefully with a uniform distribution and then compute the check digits. If \f(CW$valid\fR==0, the check digits are computed \fBnot to\fR satisfy the check equations. .SS "\s-1EXPORT\s0" .IX Subsection "EXPORT" \&\f(CW\*(C`test_cpf\*(C'\fR is exported by default. \f(CW\*(C`canon_cpf\*(C'\fR, \f(CW\*(C`format_cpf\*(C'\fR, \&\f(CW\*(C`parse_cpf\*(C'\fR and \f(CW\*(C`random_cpf\*(C'\fR can be exported on demand. .SH "THE CHECK EQUATIONS" .IX Header "THE CHECK EQUATIONS" A correct \s-1CPF\s0 number has two check digits which are computed from the base 9 first digits. Consider the \s-1CPF\s0 number written as 11 digits .PP .Vb 1 \& c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] dv[1] dv[2] .Ve .PP To check whether a \s-1CPF\s0 is correct or not, it has to satisfy the check equations: .PP .Vb 3 \& c[1]*10+c[2]*9+c[3]*8+c[4]*7+c[5]*6+ \& c[6]*5+c[7]*4+c[8]*3+c[9]*2+dv[1] = 0 (mod 11) or \& = 1 (mod 11) (if dv[1]=0) .Ve .PP and .PP .Vb 3 \& c[2]*10+c[3]*9+c[4]*8+c[5]*7+c[6]*6+ \& c[7]*5+c[8]*4+c[9]*3+dv[1]*2+dv[2] = 0 (mod 11) or \& = 1 (mod 11) (if dv[2]=0) .Ve .SH "BUGS" .IX Header "BUGS" I heard that there are exceptions of \s-1CPF\s0 numbers which don't obey the check equations and are still authentic. I have never found one of them. .SH "SEE ALSO" .IX Header "SEE ALSO" To make sure this module works, one can try the results obtained against those found with \*(L"Comprovante de Inscrição e de Situação Cadastral no \s-1CPF\*(R",\s0 a web page which the Brazilian Ministry of Revenue provides for public consultation on regularity status of the taxpayer. This page tells if the \s-1CPF\s0 number is a correct entry (11\-digits\-long with verified check digits), if it references a real person and if he/she is regular with the government body. .PP Given a bad \s-1CPF,\s0 the after-submit page tells \*(L"\s-1CPF\s0 incorreto\*(R". If the \s-1CPF\s0 is a good one but does not reference a real person, it says \*(L"\s-1CPF\s0 não existe em nossa base de dados\*(R" (\s-1CPF\s0 does not exist in our database). Otherwise, it shows a details form for the identified taxpayer. .PP Note that this module only tests correctness. It doesn't enter the merit whether the \s-1CPF\s0 number actually exists at the Brazilian government databases. .PP As you might have guessed, this is not the first Perl module to approach this kind of functionality. Take a look at .PP .Vb 2 \& http://search.cpan.org/search?module=Brasil::Checar::CPF \& http://search.cpan.org/search?query=cpf&mode=all .Ve .PP Please reports bugs via \s-1CPAN RT,\s0 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Business\-BR\-Ids By doing so, the author will receive your reports and patches, as well as the problem and solutions will be documented. .SH "AUTHOR" .IX Header "AUTHOR" A. R. Ferreira, .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2005 by A. R. Ferreira .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available. .SH "POD ERRORS" .IX Header "POD ERRORS" Hey! \fBThe above document had some coding errors, which are explained below:\fR .IP "Around line 112:" 4 .IX Item "Around line 112:" Non-ASCII character seen before =encoding in '"Ministério'. Assuming \s-1CP1252\s0