/usr/share/perl5/Business/ISMN.pm is in libbusiness-ismn-perl 1.131-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | package Business::ISMN;
use strict;
use base qw(Exporter);
use subs qw(
_common_format _checksum is_valid_checksum
INVALID_PUBLISHER_CODE
BAD_CHECKSUM
GOOD_ISMN
BAD_ISMN
);
use vars qw( $VERSION @ISA @EXPORT_OK $debug %country_data
$MAX_COUNTRY_CODE_LENGTH );
use Carp qw(carp);
use Exporter;
use List::Util qw(sum);
use Tie::Cycle;
use Business::ISMN::Data;
my $debug = 0;
@EXPORT_OK = qw(is_valid_checksum ean_to_ismn ismn_to_ean
INVALID_PUBLISHER_CODE BAD_CHECKSUM GOOD_ISMN BAD_ISMN);
($VERSION) = '1.131';
sub INVALID_PUBLISHER_CODE { -3 };
sub BAD_CHECKSUM { -1 };
sub GOOD_ISMN { 1 };
sub BAD_ISMN { 0 };
my %Lengths = qw(
0 3
1 4
2 4
3 4
4 5
5 5
6 5
7 6
8 6
9 7
);
sub new
{
my $class = shift;
my $common_data = _common_format shift;
return unless defined $common_data;
my $self = {};
bless $self, $class;
$self->{'ismn'} = $common_data;
$self->{'positions'} = [1,undef,9];
# we don't know if we have a valid publisher code,
# so let's assume we don't
$self->{'valid'} = INVALID_PUBLISHER_CODE;
# let's check the publisher code.
my $code_length = $Lengths{ substr( $self->{'ismn'}, 1, 1 ) };
$self->{publisher_code} = substr(
$self->{'ismn'},
1,
$code_length
);
my $code_end = $code_length + 1;
$self->{'positions'}[1] = $code_end;
return $self unless $self->is_valid_publisher_code;
# we have a good publisher code, so
# assume we have a bad checksum until we check
$self->{'valid'} = BAD_CHECKSUM;
$self->{'article_code'} = substr( $self->{'ismn'}, $code_end, 9 - $code_end );
$self->{'checksum'} = substr( $self->{'ismn'}, -1, 1 );
$self->{'valid'} = is_valid_checksum( $self->{'ismn'} );
return $self;
}
#it's your fault if you muck with the internals yourself
# none of these take arguments
sub ismn () { my $self = shift; return $self->{'ismn'} }
sub is_valid () { my $self = shift; return $self->{'valid'} }
sub publisher () { my $self = shift; return $self->{'publisher'} }
sub publisher_code () { my $self = shift; return $self->{'publisher_code'} }
sub article_code () { my $self = shift; return $self->{'article_code'} }
sub checksum () { my $self = shift; return $self->{'checksum'} }
sub hyphen_positions () { my $self = shift; return @{$self->{'positions'}} }
sub fix_checksum
{
my $self = shift;
my $last_char = substr($self->{'ismn'}, 9, 1);
my $checksum = _checksum $self->ismn;
substr($self->{'ismn'}, 9, 1) = $checksum;
$self->_check_validity;
return 0 if $last_char eq $checksum;
return 1;
}
sub as_string
{
my $self = shift;
my $array_ref = shift;
#this allows one to override the positions settings from the
#constructor
$array_ref = $self->{'positions'} unless ref $array_ref eq 'ARRAY';
return unless $self->is_valid eq GOOD_ISMN;
my $ismn = $self->ismn;
foreach my $position ( sort { $b <=> $a } @$array_ref )
{
next if $position > 9 or $position < 1;
substr($ismn, $position, 0) = '-';
}
return $ismn;
}
sub as_ean
{
my $self = shift;
my $ismn = ref $self ? $self->as_string([]) : _common_format $self;
return unless ( defined $ismn and length $ismn == 10 );
# the M becomes a zero in bookland
substr( $ismn, 0, 1 ) = '0';
my $ean = '979' . substr($ismn, 0, 9);
my $sum = 0;
foreach my $index ( 0, 2, 4, 6, 8, 10 )
{
$sum += substr($ean, $index, 1);
$sum += 3 * substr($ean, $index + 1, 1);
}
#take the next higher multiple of 10 and subtract the sum.
#if $sum is 37, the next highest multiple of ten is 40. the
#check digit would be 40 - 37 => 3.
$ean .= ( 10 * ( int( $sum / 10 ) + 1 ) - $sum ) % 10;
return $ean;
}
sub is_valid_publisher_code
{
my $self = shift;
my $code = $self->publisher_code;
my $success = 0;
foreach my $tuple ( @publisher_tuples )
{
no warnings;
next if( defined $tuple->[2] and $code > $tuple->[2] );
last if $code < $tuple->[1];
if( $code >= $tuple->[1] and $code <= $tuple->[2] )
{
$success = 1;
$self->{'publisher'} = $tuple->[0];
last;
}
}
return $success;
}
sub is_valid_checksum
{
my $data = _common_format shift;
return BAD_ISMN unless defined $data;
return GOOD_ISMN if substr($data, 9, 1) eq _checksum $data;
return BAD_CHECKSUM;
}
sub ean_to_ismn
{
my $ean = shift;
$ean =~ s/[^0-9]//g;
return unless length $ean == 13;
return unless substr($ean, 0, 3) eq 979;
#XXX: fix to change leading 0 back to M
my $ismn = Business::ISMN->new( substr($ean, 3, 9) . '1' );
$ismn->fix_checksum;
return $ismn->as_string([]) if $ismn->is_valid;
return;
}
sub ismn_to_ean
{
my $ismn = _common_format shift;
return unless (defined $ismn and is_valid_checksum($ismn) eq GOOD_ISMN);
return as_ean($ismn);
}
sub png_barcode
{
my $self = shift;
my $ean = ismn_to_ean( $self->as_string([]) );
eval "use GD::Barcode::EAN13";
if( $@ )
{
carp "GD::Barcode::EAN13 required to make PNG barcodes";
return;
}
my $image = GD::Barcode::EAN13->new($ean)->plot->png;
return $image;
}
#internal function. you don't get to use this one.
sub _check_validity
{
my $self = shift;
if( is_valid_checksum $self->{'ismn'} eq GOOD_ISMN
and defined $self->{'publisher_code'} )
{
$self->{'valid'} = GOOD_ISMN;
}
else
{
$self->{'valid'} = INVALID_PUBLISHER_CODE
unless defined $self->{'publisher_code'};
$self->{'valid'} = GOOD_ISMN
unless is_valid_checksum $self->{'ismn'} ne GOOD_ISMN;
}
}
#internal function. you don't get to use this one.
sub _checksum
{
my $data = _common_format shift;
tie my $factor, 'Tie::Cycle', [ 1, 3 ];
return unless defined $data;
my $sum = 9;
foreach my $digit ( split //, substr( $data, 1, 8 ) )
{
my $mult = $factor;
$sum += $digit * $mult;
}
#return what the check digit should be
# the extra mod 10 turns 10 into 0.
my $checksum = ( 10 - ($sum % 10) ) % 10;
return $checksum;
}
#internal function. you don't get to use this one.
sub _common_format {
no warnings qw(uninitialized);
#we want uppercase X's
my $data = uc shift;
# get rid of everything except decimal digits and X
# and leading M
$data =~ s/[^0-9M]//g;
return $1 if $data =~ m/
^
(
M
\d{9}
)
$
/x;
return;
}
1;
__END__
=encoding utf8
=head1 NAME
Business::ISMN - work with International Standard Music Numbers
=head1 SYNOPSIS
use Business::ISMN;
$ismn_object = new Business::ISMN('M021765430');
$ismn_object = new Business::ISMN('M-021-76543-0');
#print the ISMN with hyphens at positions specified
#by constructor
print $ismn_object->as_string;
#print the ISMN with hyphens at specified positions.
#this not does affect the default positions
print $ismn_object->as_string([]);
#print the publisher or publisher code
print $ismn->publisher;
print $ismn->publisher_code;
#check to see if the ISMN is valid
$ismn_object->is_valid;
#fix the ISMN checksum. BEWARE: the error might not be
#in the checksum!
$ismn_object->fix_checksum;
# create an EAN13 barcode in PNG format
$ismn_object->png_barcode;
#EXPORTABLE FUNCTIONS
use Business::ISMN qw( is_valid_checksum
ismn_to_ean ean_to_ismn );
#verify the checksum
if( is_valid_checksum('0123456789')
eq Business::ISMN::GOOD_ISMN )
{ ... }
#convert to EAN (European Article Number)
$ean = ismn_to_ean('1565921496');
#convert from EAN (European Article Number)
$ismn = ean_to_ismn('9781565921498');
=head1 DESCRIPTION
=head2 Methods
=over 4
=item new($ismn)
The constructor accepts a scalar representing the ISMN.
The string representing the ISMN may contain characters
other than C<[0-9mM]>, although these will be removed in the
internal representation. The resulting string must look
like an ISMN - the first character is an 'M' and the
following nine characters must be digits.
The constructor attempts to determine the country
code and the publisher code. If these data cannot
be determined, the constructor sets C<$obj-E<gt>is_valid>
to something other than C<GOOD_ISMN>.
An object is still returned and it is up to the program
to check C<$obj-E<gt>is_valid> for one of five values (which
may be exported on demand). The actual values of these
symbolic versions are the same as those from previous
versions of this module which used literal values.
Business::ISMN::INVALID_PUBLISHER_CODE
Business::ISMN::BAD_CHECKSUM
Business::ISMN::GOOD_ISMN
Business::ISMN::BAD_ISMN
The string passed as the ISMN need not be a valid ISMN as
long as it superficially looks like one. This allows one to
use the C<fix_checksum()> method. Despite the disclaimer in
the discussion of that method, the author has found it
extremely useful. One should check the validity of the ISMN
with C<is_valid()> rather than relying on the return value
of the constructor. If all one wants to do is check the
validity of an ISMN, one can skip the object-oriented
interface and use the C<is_valid_checksum()> function
which is exportable on demand.
If the constructor decides it cannot create an object, it
returns C<undef>. It may do this if the string passed as the
ISMN cannot be munged to the internal format meaning that it
does not even come close to looking like an ISMN.
=item ismn
Returns the ISMN as a string
=item publisher
Returns the country associated with the publisher code.
=item publisher_code
Returns the publisher code or C<undef> if no publisher
code was found.
=item article_code
Returns the article code or C<undef> if no article
code was found.
=item checksum
Returns the checksum or C<undef> if no publisher
code was found.
=item hyphen_positions
Returns the list of hyphen positions as determined from the
country and publisher codes. the C<as_string> method provides
a way to temporarily override these positions and to even
forego them altogether.
=item as_string(), as_string([])
Return the ISMN as a string. This function takes an
optional anonymous array (or array reference) that specifies
the placement of hyphens in the string. An empty anonymous array
produces a string with no hyphens. An empty argument list
automatically hyphenates the ISMN based on the discovered
publisher code. An ISMN that is not valid may
produce strange results.
The positions specified in the passed anonymous array
are only used for one method use and do not replace
the values specified by the constructor. The method
assumes that you know what you are doing and will attempt
to use the least three positions specified. If you pass
an anonymous array of several positions, the list will
be sorted and the lowest three positions will be used.
Positions less than 1 and greater than 9 are silently
ignored.
=item is_valid
Returns C<Business::ISMN::GOOD_ISMN> if the checksum is valid and the
country and publisher codes are defined.
Returns C<Business::ISMN::BAD_CHECKSUM> if the ISMN does not pass
the checksum test. The constructor accepts invalid ISMN's so that
they might be fixed with C<fix_checksum>.
Returns C<Business::ISMN::INVALID_PUBLISHER_CODE> if a publisher code
could not be determined.
Returns C<Business::ISMN::BAD_ISMN> if the string has no hope of ever
looking like a valid ISMN. This might include strings such as C<"abc">,
C<"123456">, and so on.
=item is_valid_publisher_code
Returns true if the publisher code is valid, and false otherwise.
=item fix_checksum()
Replace the tenth character with the checksum the
corresponds to the previous nine digits. This does not
guarantee that the ISMN corresponds to the product one
thinks it does, or that the ISMN corresponds to any product
at all. It only produces a string that passes the checksum
routine. If the ISMN passed to the constructor was invalid,
the error might have been in any of the other nine positions.
=item $obj-E<gt>as_ean()
Converts the ISMN to the equivalent EAN (European Article Number).
No pricing extension is added. Returns the EAN as a string. This
method can also be used as an exportable function since it checks
its argument list to determine what to do.
=item png_barcode()
Creates a PNG image of the EAN13 barcode which corresponds to the
ISMN. Returns the image as a string.
=back
=head2 EXPORTABLE FUNCTIONS
Some functions can be used without the object interface. These
do not use object technology behind the scenes.
=over 4
=item is_valid_checksum('M021765430')
Takes the ISMN string and runs it through the checksum
comparison routine. Returns C<Business::ISMN::GOOD_ISMN>
if the ISMN is valid, C<Business::ISMN::BAD_CHECKSUM> if the
string looks like an ISMN but has an invalid checksum, and
C<Business::ISMN::BAD_ISMN> if the string does not look like
an ISMN.
=item ismn_to_ean('M021765430')
Takes the ISMN string and converts it to the equivalent
EAN string. This function checks for a valid ISMN and will return
undef for invalid ISMNs, otherwise it returns the EAN as a string.
Uses as_ean internally, which checks its arguments to determine
what to do.
=item ean_to_ismn('9790021765439')
Takes the EAN string and converts it to the equivalent
ISMN string. This function checks for a valid ISMN and will return
undef for invalid ISMNs, otherwise it returns the EAN as a string.
Uses as_ean internally, which checks its arguments to determine
what to do.
=back
=head1 TO DO
* i need more ISMN numbers for tests
=head1 SOURCE AVAILABILITY
This source is in Github:
http://github.com/briandfoy/business--isbn/tree/master
=head1 AUTHOR
brian d foy, C<< <bdfoy@cpan.org> >>
=head1 COPYRIGHT AND LICENSE
Copyright © 2001-2016, brian d foy <bdfoy@cpan.org>. All rights reserved.
You may redistribute this under the same terms as Perl itself.
=cut
|