If you only had one programming language to choose –or– Let the FUD be with you

Published · Sunday, 5 July 2009 (Updated · 7 March 2010)

In Babu Srinivasan’s discussion on the topic of what programming language could theoretically be the the sweetest choice—If you have to learn just one programming language—we see decent coverage of the concerns involved.

What we also see, however, is the same reflexive, fact-depleted dismissal of Perl based on third hand complaints by second rate hackers.

…many people consider it to be line noise…

If you have to learn just one programming language

This is what is technically known as weasel-words. There’s more where that came from. He points out that Perl is strictly a legacy language now; no new software being written in it. For example the BBC’s new iPlayer which runs on the same Perl framework Sedition·com does was written via time machine by hackers who travelled to the past where Perl is still in use.

What really burns is that I know what readers think when they read the article. No one is convinced by his conclusion because he doesn’t pick C#, Ruby, Java, or Python. So all those guys are going, “Oh, that’s totally right. Perl sucks and all the others are losers too except for (C#|Ruby|Java|Python), the lingo in which I hack. He just didn’t get the bigger picture. Of course (C#|Ruby|Java|Python) is the right choice.”

Anyway, the closer is: “Scala wins and here’s an example of why: <code/>.” Go look at it. Then come back and look at this–

Dice game Perl

use strict;
use Pod::Usage;
use Getopt::Long;
use Number::Format qw( format_number format_price );

my ( $plays, $bet );
GetOptions ( "plays=i" => \$plays,
             "bet=f"   => \$bet
           );

$plays and $bet or pod2usage(-verbose => 2);

my $start_money = my $money = $plays * $bet;

for ( 1 .. $plays ) {
    my $roll = int( rand(100) ) + 1;
    if ( $roll <= 50 ) {
        $money -= $bet;
    } elsif ( $roll >= 66 and $roll <= 75 ) {
        $money += 0.5 * $bet;
    } elsif ( $roll >= 76 and $roll <= 99 ) {
        $money += $bet;
    } elsif ( $roll == 100 ) {
        $money += 2 * $bet;
    }
}

printf("%s became %s after %s play%s:\n\t",
       format_price($start_money),
       format_price($money),
       format_number($plays),
       $plays == 1 ? "" : "s");

printf("You get %s for a dollar\n",
       format_price($money / $start_money));

=head1 Question: Do you want to play this game?

You put down some money and roll a die between 1 and
100. Depending on your roll-
       1-50 --> I keep your money
      51-65 --> You get your money back
      66-75 --> You get 1.5 times your money
      76-99 --> You get 2 times your money
        100 --> You get 3 times your money

B<Arguments>-
     -plays    How many times to play
     -bet      How much to bet on each play

I<Do you want to play?>

=cut

You’ll note that as reported, although they read almost exactly the same, the Perl is unreadable. Or is it the Scala? Damnit, now I don’t know what how to think for myself.

Additionally, the Perl is a complete program runnable with flexible, type-filtered arguments. The Scala requires more code be written to run with other parameters. The Perl is self-documenting—code comments are not documentation. And the Perl correctly formats numbers and currency based on locale—halfway to i18n without hardly trying. Three big improvements over the Scala version and in nearly the same space with some easier reading, frankly. and and or read much more naturally than && and || and make lots of code less cluttered because their weaker precedence allows the omission of parenthesis.

Usage

jinx@jasper[2260]~>dice-game -bet 1 -plays 100
USD 1,100.00 became USD 850.00 after 11 plays:
        You get USD 0.77 for a dollar

jinx@jasper[2261]~>dice-game -bet 1 -p 100
USD 100.00 became USD 74.50 after 100 plays:
        You get USD 0.75 for a dollar

jinx@jasper[2262]~>dice-game -b 7 -plays 100000
USD 700,000.00 became USD 564,413.50 after 100,000 plays:
        You get USD 0.81 for a dollar

jinx@jasper[2263]~>dice-game -b 1.01 -p 1
USD 1.01 became USD 2.02 after 1 play:
        You get USD 2.00 for a dollar

Not convinced? I know, logic and proof is difficult for human beings to follow. You get emotions though, right?

Perl hackers are happy hackers.

PS: You can embed Java, PHP, C, C++ and more in Perl and any time someone tells you something positive about another language ask them, “Can Perl do that too?” And when they say, “No,” walk away ’cause they don’t know what the ƒµ¢%1 they’re talking about.

PPS: We also note that the article in favor of Scala was delivered by a site coded in PHP. Of course it’s too much to expect one to code one’s own blog in one’s favored language no matter how convinced one is that said language is ideal… Well, you know, unless it’s Perl, I guess. I wrote this one and I did it in a month because Perl and its many packages are that easy and fun to use.

PHPS: If this has led you to conclude Perl hackers are asocial jerks, rest assured they are not. Not all of us, anyway.

1 I got your line noise right here.



digg stumbleupon del.icio.us reddit Fark Technorati Faves

« Perl snippet for the weekend: getting “normalized” column names to act like proper object identifiers with DBIx::Class::Schema::Loader · 10 Catalyst models in 10 days1 »
« Perl resources, modules, and sample code »