October 2010 Archives

Perl101: finding words in words

Recently a friend of mine back in the US mentioned an art project she was working on. She was looking for words which are composites of two words. She didn't necessarily want obvious composites like "bluebird", but less obvious composites that can be worked into her art project. For example, "homeless" could be "home less", or "garbage" to "garb age". A few people struggled to come up with examples. I came up with /usr/share/dict/words and a few lines of Perl. I use a few nifty idioms that every Perler should be familiar with.

Perl 101: What Do You Want?

I may be out of touch for a bit as I'm moving to Amsterdam tomorrow night, but in the meantime, tell me what you would like to see for "Perl 101" blog posts. Every time I post something with the newbies tag (which I'm going to change to the friendlier "perl101"), I get a fair number of comments and the post shows up a few times on Twitter. Since I'm getting a good response, I'm assuming that people actually like these posts and want to see more of them.

So tell me what you want and I'll see what I can do.

Storable: "freeze" versus "nfreeze"

I was doing a code review and discovered that one of our developers wrote code using Storable's freeze() function. This turned out to be a bug because we store objects in memcache with nfreeze() instead. Storable's docs have only this to say about nfreeze().

If you wish to send out the frozen scalar to another machine, use "nfreeze" instead to get a portable image.

Since people generally use freeze() instead, I decided to dig around and figure out what was going on. After all, if nfreeze() is portable, there must be a price to pay, right?

Perl 101: avoid "elsif"

We had some code which looked (sort of) like this:

if ( $ondemand->service eq 'iplayer_broadband_streaming' ) {
elsif ( $ondemand->service eq 'iplayer_streaming_h264_flv' ) {
elsif ( $ondemand->service eq 'iplayer_streaming_h264_flv_high' ) {
elsif ( $ondemand->service eq 'iplayer_streaming_h264_flv_hd' ) {
elsif ( $ondemand->service eq 'iplayer_streaming_h264_flv_lo' ) {
elsif ( $ondemand->service eq 'iplayer_mobile_wmv' ) {
elsif ( $ondemand->service eq 'iplayer_broadband_download' ) {
else ( $ondemand->service eq 'iplayer_download_http_h264_air' ) {

As a general rule of thumb, I recommend that when you use an "if" statement, you keep it as simple as possible:

if true 
    do something
end if


if true 
    do something
    do something else
end if

Long if/elsif/end chains like you see above have a few issues:

  • Duplicate code
  • Harder to maintain
  • Confusing to read (there's a probable bug in that code)

Let's look at these.

The Lacuna Expanse

Another obligatory Lacuna Expanse post. JT Smith announced the creation of Lacuna Expanse, a free online game with the backend written in Perl. Most free online games are rather dull (I've tried a few), but this one shows promise.

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/