Archive for January, 2008

is perl still worth learning

Someone entered “is perl still worth learning” into a search engine and found my blog.

The answer is Yes.

Python and Ruby are not inherently bad, but Perl is at least as useful and modern as them, it has – arguably – a wonderful community of programmers, it has an amazing library of reusable modules called CPAN.

My wife Hadar is starting serious work on her PhD in physics in the Technion. The guys in the lab in which she will be working wrote some calculations software in Fortran on Windows. The first thing that Hadar is doing is deciphering this Fortran code. She asked me for some help, and i couldn’t provide much, because i don’t really know Fortran. I suggested that she will advise those lab guys to consider porting their software, at least for the future, to Perl, because it is portable and because it is quite possible that it has the same capabilities for mathematical and scientific work as Fortran has. She told it to one of the researchers there and he replied that it should not be done, because “Perl is just a language for network servers.”

Saying that “Perl is just a language for network servers” is pretty much like saying that all Russian women are prostitutes. It’s a sad and silly prejudice. Here’s an article that dispels it: Ten Perl Myths.

So Hadar learned a little Perl and PDL – the Perl library for advanced mathematics. She picked up the basics very quickly. I was pleasantly surprised that she found that Perl’s main data types are scalars ($drug = 'caffeine') and arrays (@drugs = ('marijuana', 'quaalude', 'paracetamol')), because in math it works the same way (we didn’t discuss hashes yet). I was even more surprised to learn that it seemed perfectly fine to her that @drugs is an array, but to access ‘quaalude’ you need to write $drugs[2] and not @drugs[2]. We tried searching CPAN for various mathematical functions, such as eigenvalue, matrix diagonal and linear algebra, and found everything.

So she’s gonna try that.

If she can’t convince them to migrate to Perl, i’ll have to learn Fortran and try to help them migrate from a Windows version of Fortran to GNU Fortran.

Nu pogodi

You ran away this time, but i’ll catch you one day.

Thinking About the Spammer

Once i used to read most of the spam that i received. Then it became too much and i started reading only spam in Hebrew, which is quite different from the English and Chinese varieties. But now even that is too much. Lately i ignore most of it. It’s a bit unfortunate, because it is a curious bit of human culture.

Today i received a message with this subject: “Gay Sex Tanned guy bangs a friends ass”. The content was auto-generated near-gibberish of weird words, such as “blackstrap ghostless shikargah , spermatocidal sahara dewormed”. I’s probably supposed to trick spam filters. But there was nothing except it: No dirty images, no link to a porno site, no advertising.

Most people don’t give a damn, but i wonder: Is it just totally pointless waste of bandwidth? Is it supposed to trick me into trying to reply to this email and verify that my email address is right? Did the spammer send the message with a link to a porno site, but it was deleted by some filter? Was the spammer supposed to send it with a link but made a mistake? Did he program the spamming robot incorrectly?

A spammer is human, too.

Binary

I program for living, but i’ve never received proper formal education in serious algorithms.

Here’s a very simple problem: Take an array of length n and fill it with zeros. Every array member represents a binary digit. Now, using this array and not using the usual math for binary conversion, print all binary digits from zero to the maximum binary number with n digits. For example, with n == 3 this should be printed:

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

Here’s what i wrote. Is it OK or is it an embarrassment?

use strict;
use warnings;

# That's right, upgrade to Perl 5.10.
# If you can't, comment out this line.
use 5.010;

my $digits = $ARGV[0] // 3;

# /If you don't have Perl 5.10, use this:
# my $digits = defined $ARGV[0] ? $ARGV[0] : 3;

my @matrix = ();
my @number = map { 0 } (1 .. $digits);
my $last = 0;

NUMBER:
while (not $last) {
    push @matrix, [ @number ];

    my $digit_index = $digits;
    DIGIT:
    while ($digit_index) {
        $digit_index--;
        $last = 1;

        if ($number[$digit_index]) {
            $number[$digit_index] = 0;
        }
        else {
            $last = 0;
            $number[$digit_index] = 1;
          next NUMBER;
        }
    }
}

foreach my $number (@matrix) {
    print "@{$number}\n";
}

Oh (edit): The real embarrassment – in WordPress the sourcecode presentation cannot display Perl properly. But if i put ‘ruby’ instead of ‘perl’ in the language attribute, it works mostly fine …



Follow

Get every new post delivered to your Inbox.

Join 1,699 other followers