Luhn algorithm in PHP

As I mentioned before, the Luhn algorithm is used to validate some interesting numbers, most notably GSM IMEIs and credit card numbers. Here’s another implementation I wrote, this time in PHP.

	function luhn($str) {
		$odd = !strlen($str)%2;
		$sum = 0;
		for($i=0;$i<strlen($str);++$i) {
			if($odd) {
			} else {



Are you wondering where the heck does some arcane module come from? Wonder no more, perlwhich comes to the rescue:

use strict;
use File::Spec;

my $module = shift @ARGV;
my $pm=$module.'.pm';
my @path = split(/::/,$pm);
my $found = 0;
foreach my $dir (@INC) {
        my $file = File::Spec->catfile($dir,@path);
        if(-f $file) {
                print $file,"\n";

Next time you need to know some module’s path just run it like this:

$ perlwhich Data::Dumper

If a module resides in multiple locations under @INC, perlwhich will let you know as well:

$ perlwhich Salesforce

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.


A small filter to phonetize STDIN into the NATO phonetic alphabet. It’s very useful to spell passwords over the phone.

use strict;
my %ALPHA = map {uc(substr($_,0,1))=>$_} qw( Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu );
while(<STDIN>) {
        print $_, "\n";
        print join(' ',map {$ALPHA{uc($_)}||$_} (split(//,$_))),"\n";

Here’s the source code.
Yup, I know about Lingua::Alphabet::Phonetic::NATO but I needed a quick script without module dependencies. Yes, I haven’t been able to learn the NATO phonetic alphabet yet -unlike some geeks with too much time in their hands (you know who you are)- and yes, I know I’m lazy, but that’s why I am a perl fan anyway ;-) .
There’s more information about this alphabet atWikipedia. You migth want to donate a few bucks while you’re there.

Los Sangre are coming for you

mind-on-fearJust in time for Halloween. Last Thursday I didn’t get my fix of morning radio, so this note in came as something of a surprise. “Beware. Los Sangre are coming for you”. The baloney detection device went off immediately, and a quick visit to confirmed this as a well-documented urban legend. That didn’t stop the media from spreading the word -the rumour was legitimized by an official press release, after all- and further questioning to other officials in the federal and state level confirmed the presence of the gang in the country. “We are taking it seriously” sounds good enough. After all, what government spokesperson is willing to admit that he doesn’t have a clue about whatever the hell you’re asking him about?. It doesn’t hurt to say that “the (National Police Force) is on alert” and that “(the Federal Government) is informed about the situtation”, does it?
It’s not the first time that the general insecurity in Mexico contributes to give some plausability to an incredible story, and it certainly won’t be the last. Here lawlessness defies imagination and impunity is taked for granted. Take into account that criminals can break into a government facility and take back the smuggled merchandise that’s been impounded from them, and then the rumour of a macabre gang initiation game may start to seem plausible. In a country where crime statistics makeup and official coverups are routine, if some random government official tells me to be on the lookout for a specific gang ritual, he may have a good reason for it. We forget that the work of the government agencies -what *we* actually pay them for- is to give us security, not to blow out of proportion whatever chain mail “alert” falls into their inbox. I guess that’s criminal investigation in the third world amounts to. What’s next? A press release advising the population to be on the lookout for AIDS pin pricks in movie theaters?. “Hey, it may or may not be true, but we are alert just in case. You have been warned.”. I guess that this way nobody can blame them if quantum probabilities reverse unexpectedly and the rumour just happens to be true. Heck, even a false sense of security is better than having The Government issuing bogus alerts that send the population into panic. Understandably, the population is totally psyched out. Since the story has been all over the radio and TV, everyone from laymen to well-studied intelligent people is behaving in a totally unrational way. This is your primal mind on fear. That’s what panic does for you.
Now some media outlets are waking up to the fact that this is an urban legend that’s been repeating for some time in different countries and there have been mentions of Snopes in a couple of articles. However, the undertone in some of them is unnerving: “American site dismisses a very real threat”, is my lecture of some of them. Hey, these guys should know better. Next time someone refuses to compare bloggers with journalists I might be tempted to take it as a compliment.
Now why would a government official issue a press release without further research? Because the alert came from the Interpol office in a nighbourging country, that’s why (by the way, Guatemala officials show a healty skepticism) and because the head of the state government “got the alert in his computer”. I’d really like to know *who* jumped the gun, because so far I haven’t been able to find a copy of the press release that started it all, the initial vector for this particular outbreak, although some stories point to some “DGA-PII/1548/2005” document. We better find it quickly, because once the situation comes out as it is nobody is going to take the blame. Never mind the racial undertones. The fact that officials in other countries have fell for this urban legend before doesn’t excuse ours from crying wolf over a lie, and whoever is responsible should be removed from office at once (*Ha!*).
Now for the final outcome I can only imagine what the justification will be for falling for an obviously bogus chain mail. Already, some officials have justified the heihgtened security alert by saying that they’re trying to stop copycat killers or outright pranksters. Some will say that this was an obvious cartel-backed maneuver masterminded to distract the police, or some form of guerilla warfare. Some will even talk about cyberterrorism. Or blame the media. Or Interpol. Or Guatemala. Or play semantics. I bet that others will say that this might be a conspiracy to destabilize the country — as if we needed further help with that. I’ll just stick to a corollary of Ockham’s razor in this: Never attribute to malice that which is adequately explained by stupidity.

Search for victims of Hurricane Stan

The government of the State of Chiapas has recently published on the web a list of the shelter where each victim of Hurricane Stan is right now, so their relatives can find out where they are and how to get in touch with them.
It is a good idea and undoubtel it was done in a hurry, but it is poorly implemented: the list is published in two formats: a 1.2MB PDF file and a 5.6MB HTML file (in the site the file size is listed at22MB!) that were exported directly from Microsoft Excel. If you are looking for a relative whose fate remains uncertain, it is kind of heartless to force you to download a 8MB PDF Viewer or a 5.6MB file to even start looking for your loved ones. Even more if the server doesn’t support content negotiation for GZIP compression, which would shrink the file to 262KB — under 5% of the original size! (bzip2 compression is even more efficient, the compressed file would be half that size). I will spare you of the rant about Microsoft’s disgusting HTML format.
Well, I downloaded the file and wrote a small perl script to clean up that file and import the records into a SQL database. It even does Soundex translation for improved accuracy thanks to Text::Soundex. Perl is Beautiful. If you can, you may want to publish that database on your own site to give people an opportunity to find their relatives, or point them to where the search will be kept indifinitely and the database will be refreshed as needed.
BTW, my relatives reported while Stan was still in full force and all of them turned out unharmed.

Changing TiVo’s timezone and date/time

To change your TiVo’s timezone and date/time follow these steps:

  1. Get timezoneadj30.tcl. I don’t have access to tivo_canada’s archive, so I had to write my own timezoneadj30.tcl.
  2. Edit it and change the timezone. It seems that 0=GMT,3=EST
  3. Run ./timezoneadj30.tcl
    # ./timezoneadj30.tcl
  4. Set the clock using settime, e.g.
    # settime 20031123025900
    # settime -rtc
  5. I noticed that if you set the time back, the IR blaster stops working. I’m not proud of my solution:
    # reboot

Note: This post was originally listed under the static TiVO page, but I’ll start managing the TiVo section with WordPress.