Archive for the 'Perl' Category

A Fresh Look at Efficient Perl Sorting

A Fresh Look at Efficient Perl Sorting - Ah, perl! This is exactly the sort of thing I love about you.

Soft spot or blind spot?

Recently I've been looking into other languages and talking about them with friends, but we keep coming back to the same realization: for all of its warts, we're always going to always have a soft spot for Perl. Here's just one of the many perlisms I'd miss in another language:

    # create a new CGI object with three levels of masking:  
    # defaults get masked by parameters, and parameters get masked by overrides
    my $cgi = new CGI( {  %$defaults_ref,
                          %$parameters_ref,
                          %$overrides_ref,
                       } );

Thing I hate about Java #2384

I can't even put into words how crazy it makes me that java doesn't require { }'s around its control blocks.

if ( bool )
     if ( bool2) 
          something();
     else 
          somethingelse();
else
     shootme();

That's completely valid and perfectly awful in java. I'm regularly looking at a lot of new java code at work, and every time I come across that it takes me a few extra seconds to read it. And the people who wrote the code seem awfully fond of that six-character saving construction. They also don't always indent it usefully, so I frequently find myself reading the wrong else at first glance. Also, without the { }'s, how is my editor supposed to highlight the matching curly to help me see where a block is?

While I completely understand the argument that control structures take a single statement, and a single statement can either be a semicolon terminated line or a block containing multiple statements, that doesn't make it right. Look how much easier this is to read:

if ( bool ) {
     if ( bool2) {
          something();
     } else { 
          somethingelse();
     }
} else { 
     shootme();
}

Arguably, the problem I'm having is actually that people just didn't indent their code well, and not that they didn't use curlies. Python doesn't require curlies, and its control structures tend to be reasonable readable thanks to forced indentation. While Java never could have gotten away with adding forced indentation (very few C programmers ever would have tried java in the first place, then), it probably could have gotten away with requiring curlies, and that would have helped enforce a little extra readability.

I'm so glad perl requires { }'s in that context. Perl's something() if bool; construction is also pretty kickass, and much better thanks to its lack of an "else." But then again, perl also has elsif, which is a fairly questionable design decision in the control structure domain.

The Hundred-Year Language

The Hundred-Year Language

This is a really cool article about what programming languages might be like 100 years from now. My favorite quote in the article is "There are some stunningly novel ideas in Perl, for example. Many are stunningly bad, but that's always true of ambitious efforts. At its current rate of mutation, God knows what Perl might evolve into in a hundred years."

LinkStew Digest, October 23rd, 2002 Edition

Instead of one single item, I'll make you a deal and give you 7 short items instead. Consider it one for each day I haven't blogged in the last week. ;-)

  • Metroid Prime comes out in a little less than a month, and Nintendo just posted the commercial. It's a pretty nice commercial (much better than that awful Super Mario Sunshine commercial), and it got me even more excited about the game. Hopefully it gets some non-Nintendo folks to check out the Gamecube.
  • Check out the video of Verbal, the single of Amon Tobin's new album Out From Out Where.
  • If you're wondering, I didn't go see Stallman speak -- Instead, I went and listened to Lakoff talk about causality and the mind for 80 minutes.
  • Just like Kevin got a new Wells Fargo Platinum Visa card a few weeks ago, I got my new Bank of America Visa Platinum Check Card today. Unlike Kevin, though, this is actually exciting for me. Not only does this increase the amount I can spend at once from $500 to $1200, but since my card number changed, it also means I'll finally be able to use Paypal.
  • I'm happy to see the World Series tied 2-2. On the one hand, I want the Giants to win it all because of that Disney thing with the Angels, and because duh, it's San Francisco. On the other hand, I want the Angels to win it all because I like the AL better, and I like seeing underdogs do well, and because all the news about the Giants seems to be Bonds Bonds Bonds. Really, I want to see it go to Game 7 again.
  • As for that First Circle concert I went to a couple of weeks back, it was pretty good. First Circle didn't come on until after 12:30, but they played until 2. And between First Circle and the two other bands that played, we got around 4 hours of music for a mere $6. I highly recommend checking out concerts at Blake's, and I think I'll have to try to get back there a few more times before I'm done here.
  • Oh, and finally, check out the Perl IAQ - Infrequently Asked Questions. It's pretty amusing. I especially like the answer for "How do I convert a string to a number?"

Perl vs. Excel, Round 1, Fight!

I was working with a bunch of Excel data this weekend for my CS project, and at some point I decided I wanted to transpose a table of data. You know, swap the rows and the columns? Simple, right?

Well, I tried searching Excel help, and had no luck. A quick google search didn't immediately yield the answer. And I asked my CS partners, but they also had no clue. A few of them even asked other people they knew in the lab, and still no luck.

For a bunch of CS majors, I was amazed by how little they all knew about Excel. Hell, that I was the one manning Excel in my group (because I knew the most about it) kinda says a lot by itself. I mean, I can get around in Excel, but I wouldn't dare list Excel on my resume. I know how complicated that sucker is, and I know that I can barely scratch the surface.

So I shrugged and decided I was going to write a perl script to transpose colon-delimited data that I could export from Excel. Perl is, afterall, my swiss-army chainsaw. What's the point of having a chainsaw if you're not going to use it? And besides, I figured it wouldn't take me more than 5 minutes to write, and I'd already spent at least that long trying to figure out how to do it with Excel.

But before I started, I had to go to the bathroom. And when I got back, one of my partners had found the "Paste Special" option, which, among a bunch of other handy features, includes a "Transpose" option. So much for showing off my perl-fu.

Right is the opposite of wrong. (Or left.)

From Programming Perl, 3rd Edition:

    unsift
    unshift ARRAY, LIST

    This function does the opposite of shift. (Or the opposite of push, depending on how you look at it.) ...

And though that's true, that's a really problematic description, because in those terms, pop also does the opposite of shift. (Or the opposite of push, depending on how you look at it.) And knowing the difference between pop and unshift is kind of important.

Luckilly, the book does't describe pop in those terms, and the book's unsift description goes on to give an example of exactly what it does.

Ten Perl Myths

While searching for some info on determining content similarity using perl, I came across this list of Ten Perl Myths. If anything, this is neat to look at because it has a 10 second introduction to perl references.

When I teach programmers perl, I never get into all of the details, because most of it can be deduced based on their previous programming experience. However, one thing I always go over are references, because though they're not complicated, I've never dealt with anything like perl references in any other language. I also tell them that if they only read one thing from Programming Perl, it should be the chapters on references and data structures.

Though now that I think about it, I should probably also tell them to read the chapters on regular expressions, because they're what makes perl powerful, and if a perl programmer doesn't grok the power of a regular expression, then she won't appreciate the language. But the Ten Perl Myths page mentioned that, too, so I think this year I'm doing perl training, I'm going to start by sharing these Ten Perl Myths.

eightball.pl

I rely on this thing way too much, and it being just a command line script, I can just keep rerunning it until it gives me the answer I want... But hey, I guess if it makes me feel better about something, then it's worth something, yeah?

#!/usr/bin/perl

my @eightball = ("Yes", "No Doubt", "I'm Sure of It", 
                 "Certainly", "Without a Doubt",
                 "No", "No Way in Hell", "Not likely", 
                 "Keep Dreaming", "Ask Later", 
                 "Try Again");

print "$eightball[rand(@eightball)]\n";

I Like to Call It… The Stew

Instead of posting over the last couple of days, I've been working like mad towards realizing one of the major goals of LinkStew -- A living information network, which calculates relationships between entries based on a number of heuristics, in the hopes of presenting readers with links to entries directly relevant to the post that was just read. Think of it like Amazon.com's "Recomendations" feature -- "If you enjoyed reading this entry, you might also enjoy reading these other entries."

So far, only about 1/5 of all the entries in the Stew implement the feature, but almost all of them in the last couple of months work. To access The Stew, you just follow a link to a particular entry on the site (like this one or the ones in the corner of each post) and look at the list of titles following the entry.

Right now, the Stew isn't updated in real time, because I haven't yet worked in optimizations, forcing the system to work out the relationship between every node in the system. Also, it doesn't take into consideration a lot of the heuristics I plan to add in the future. For now, just take a look at the Stew and let me know if you have any suggestions or ideas, and I'll return to classifying more of my posts, hopefully making the Stew more accurate.

Arrays of Unique Elements

In doing some log parsing in perl, I found I wanted an array full of unique values. My first approach was to put all of my values in an array and then try to use a function to unique-ify the array after I'd collected all the values. I got the function written, and it wasn't overly long or complex or slow or anything, and it did it's job, and everything was good.

Until I realized that I'd reinvented a wheel. A little later in the same script, I was doing some hash work, and I realized that perl already had this really fast and highly tuned way of storing an array of unique elements. It's called a hash. Watch:

$hash_of_unique_elements{$unique_element} = "$unique_element";

Of course, the value assigned to that key could be anything. With just a few more lines of code, the value could be the number of times the element occurred. The point is, perl is already really good at doing this sort of thing, and it already accomplishes exactly what I wanted much much faster and in far fewer lines than even a highly tuned bit of code could.

Perl XML Hell

Having experienced the first hand which is the mess that is Perl XML support, an article titled What's Wrong with Perl and XML really caught my interest, and while it didn't tell much new, it was reassuring to know that I wasn't insane for thinking XML+Perl=Headaches.