NAME

Net::UPCDatabase - Simple OO interface to UPCDatabase.com

SYNOPSIS

use Net::UPCDatabase;
my $upcdb = Net::UPCDatabase->new;

print '<pre>';

print "\n[lookup]\n";
my $upc = '035000764119';
my $item = $upcdb->lookup($upc);
print "UPC: $item->{upc}\n";
if ($item->{error}) {
  print "Error: $item->{error}\n";
}
else {
  print "Product: $item->{description}\n";
  print "Size: $item->{size}\n";
}

print "\n[convertUpcE]\n";
my $upcE = '01212901';
my $upcA = $upcdb->convertUpcE($upcE);
print "UPCE: $upcA->{upcE}\n";
if ($upcA->{error}) {
  print "Error: $upcA->{error}\n";
}
else {
  print "UPCA: $upcA->{upc}\n";
}

print "\n[calculateCheckDigit]\n";
my $upcC = '01200000129C';
my $upcA = $upcdb->calculateCheckDigit($upcE);
print "UPCC: $upcA->{upcC}\n";
if ($upcA->{error}) {
  print "Error: $upcA->{error}\n";
}
else {
  print "UPCA: $upcA->{upc}\n";
}

print '</pre>';

DESCRIPTION

Connects to UPCDatabase.com to get information about a given UPC.

FUNCTIONS

new

$upcObject = Net::UPCDatabase->new;

or

$upcObject = Net::UPCDatabase->new( url => $aDifferentUrlThanDefault );

Accepts an OPTIONAL argument, a URL to use instead of the default. Unless you're really sure what you're doing, don't give it a URL. It defaults to 'http://www.upcdatabase.com/rpc', which is probably the right thing.

Returns the object.

lookup

$itemInfo = $upcObject->lookup($upc);

Accepts a REQUIRED argument, the UPC to lookup. The UPC can be either UPC-A or UPC-E.

Returns the data about the given UPC in a hash reference.

On error, it returns the given error reason as $itemInfo->{error}.

convertUpcE

$upcA = $upcObject->convertUpcE($upcE);
$isError = $upcA !~ m|^\d{12}$|;

Accepts a REQUIRED argument, the UPC-E to convert.

Returns the UPC-A (exactly 12 digits).

On error, it returns the given error reason as $itemInfo->{error}.

calculateCheckDigit

$upcA = '01200000C2X1';  # bad (more than one digit being calculated)
$upcA = '01200000C29C';  # bad (more than one digit being calculated)
$upcA = '01200000129C';  # good (only one digit)
$upcA = '0120000012C1';  # good (only one digit)
$upcA = $upcObject->calculateCheckDigit($upcA);
$isError = $upcA !~ m|^\d{12}$|;

Accepts a REQUIRED argument, the UPC-A with checkdigit placeholder (C or X) to calculate. This function will calculate the missing digit for any position, not just the last position. This only works if only one digit being calculated. This doesn't work with UPC-E. There is no difference between using "X" or "C" as the placeholder.

Returns the UPC-A with the checkdigit properly calculated.

On error, it returns the given error reason as $itemInfo->{error}.

NOTE: This uses an internal function, not the function on UPCDatabase.com because it appears that it is currently not implemented on the UPCDatabase.com side of things. If it is implemented on UPCDatabase.com, it is a simple change to use it instead.

REQUIRES

RPC::XML, RPC::XML::Client

TODO

UPC checksum checking/creation

Clean up calculation of odd-position checkdigit calculation. It currently uses an inefficient brute-force method of calculation for that position. Even-position and checksum position calculation is pretty efficient. OEOEOEOEOEOX (O=odd, E=even, X=checksum) It's not *really* that wasteful, just not as efficient as it could be.

Better documentation

Is the documentation really ever good enough?

BUGS

I am not aware of any bugs. If you find a bug, let me know!

SEE ALSO

http://www.upcdatabase.com/

AUTHOR

Dusty Wilson, <cpan-Net-UPCDatabase@dusty.hey.nu>

COPYRIGHT AND LICENSE

Copyright 2005 by Dusty Wilson

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.