Luhn algorithm in Perl

Here’s an implementation of the Luhn algorithm in perl.

use strict;
my $number = shift(@ARGV) || die("Usage: $0 <number>\n");
foreach my $n (split(//,$number)) {
        if($odd) {
        } else {
                my $x=2*$n;
my $ok = 0+(($sum%10)==0);


This program was designed for shell scripting, using something like

$ if ./luhn 457623486; then echo "ok"; else echo "error"; fi

but it should be trivial to modify it for other purposes.
I’m using this algorithm to validate GSM IMEI numbers, but the Luhn algorithm is also behind credit card numbers.

2 thoughts on “Luhn algorithm in Perl”

  1. The algorithm as published is incorrect, see this patch ;)

    – foreach my $n (split(//,$number)) {
    + foreach my $n (reverse split(//,$number)) {

Comments are closed.