The writings of Peter Stuifzand

Archive for November 2009

This is a small Perl 5.10 tip. You can use named capture buffers to fill a hash. Before 5.10 you needed to 'unpack' the values from a regex match into a hash.

I will give a small example that shows the problem this creates.

my $str = "name=value";
my %result;

if ($str =~ m/(\w+)=(\w+)/) {
    $result{name} = $1;
    $result{value} = $2;
}

The problem with this is that you need to number the variables and assign each individually to the hash. In Perl 5.10 you can do the following.

if ($str =~ m/(?<name>\w+)=(?<value>\w+)/) {
    %result = %+;
}

This is a lot simpler, cleaner and contains less possible bugs.

As a programmer I want a programmable world. The web has been a big step forward in making things programmable. Especially in the form of REST. HTTP clients can send HTTP requests to urls and proxy can filter or change what is passed through.

In the future more real world things will become programmable. For example I can program my domain name provider. As long as I supply enough money to them, I can register as many domain names as I like.

The same thing could happen with webshops and supermarkets. Each week I will send an electronic grocery list to the webshop via the web using REST and a person will deliver it the same day to my house.

The biggest problem with this approach is that people will make it harder for automated processes to interact with websites. The webs nature is to be programmable. Google (the search engine) couldn't exist without the web. They use the programmable web since the start of their company. The way they use the programmable web is the simplest way in which the web can be used. People do it every day with their browsers.

The sad thing is, that other more complicated uses of the web are made harder to do than they should be.

It seems it is now possible to share photos with MMS to Twitter. It's interesting to notice that the people at Orange UK figured out that if let people send MMS messages to an interesting service you can make a lot of money.

I have never send an MMS in my life and I probably never will, but if the use is interesting or valuable enough, I just might.

The other important thing to notice is that Orange UK can (and probably will) save half of the money (and bandwidth) that would normally be needed to send a MMS. They only have to receive the MMS from their customer. To get the photo to Twitter they only need a few servers that call the API.

I'm afraid that's a failure of your imagination. -- Rush, SG:U.

When I'm looking at code on Stackoverflow (or some other place with bad code), I'll sometimes see people asking questions about pieces of code, that I can't imagine I would have written like that in the first place.

Why is that? I just noticed it a few minutes ago, but it looks like I have to think about that some more...

This week I will show a simple example about how to use the underscore prototype which is new in Perl 5.10.

This feature was added to allow you to write functions that work like builtin functions. It's up to you to not abuse it and write unreadable code, of course.

This example shows how to use this new feature. It allows you to use the $_ variable as an argument for a user defined function.

use strict;
use feature 'say';

sub greeting(_) {
    my ($greeting) = @_; 
    $greeting //= 'world';
    say "Hello $greeting";
    return;
}

for (qw/planet people/) {
    greeting();
}

greeting();

The output of the program is:

Hello planet
Hello people
Hello world

As you can see, you don't need to specify an argument to the greeting function. The first two calls in the for loop use the two values from the loop.

The call outside the loop uses the default value ('world') that was specified in the function.

A few days ago I created two new aliases for places I connect to with ssh. Both locations have a three character alias now.

To use the aliases I created a new file called ~/.aliases. This file contains all the aliases I use. This file should be sourced in the ~/.bashrc file with the following command.

source ~/.aliases

Then add the following line to the ~/.aliases file.

alias realias='$EDITOR ~/.aliases; source ~/.aliases'

Restart bash and type realias. It will open an editor to the ~/.aliases file and source it when your done.

The ssh aliases look like this. You should change hostname1 to the new of your server.

alias ss1='ssh hostname1'

Another useful alias is lt. It shows the files in order of modification. The files that were changed last, will show up at the bottom.

alias lt='ls -lrt'

Are there any aliases that you like to use?

On Coding Horror Jeff wrote about whitespace at the end of lines of code. I hate that as much as the next guy. It seems irrational, but I think it's not. It has no function and only creates problems on times when you don't expect them, especially with source code control tools.

So I wrote a small piece of Vim script code to highlight the spaces at the end of the line in bright red.

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9

function! EndOfLineWhitespace()
    3match OverLength /\s\+$/
endfunction

call EndOfLineWhitespace()

This will highlight all the whitespace at the end of a line. If you don't like to use this for all files, you can use the usual ways to do this in Vim.

UPDATE: Since Vim 7.2 it is possible to use another function instead of the match functions. It's called matchadd. The solution I gave above can be written as follows:

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
call matchadd('OverLength', '\s\+$', -1)

See the help documentation for matchadd to see how this works.

Via VimTip810

In the latest version of Perl 5 the regex engine also got a big upgrade. There are many changes that made it faster and more correct for certain regexes. This time I will explain the new features called Named capture buffers.

Named capture buffers are similar to the numbered capture buffers, like $1 and $2. The named versions of these work the same except that you can give them a name like name or value. This will help you with documenting the regex that you use.

Here is a small example:

use 5.010;

# The regex with named capture buffers
my $regex = qr{(?<name>\w+)=(?<value>\d+)};

# For testing
my @lines = ('hello=1', 'test=2', 'perl=5010');

# The test program
for (@lines) {
    if (m/$regex/) {
        say 'Name: ', $+{name}, "\tValue: ", $+{value};
    }   
}

The output of the program:

$ perl namevalue.pl
Name: hello Value: 1
Name: test  Value: 2
Name: perl  Value: 5010

The syntax for specifying the buffers in the regex is:

(?<name>pattern)

The name should match /^[_A-Za-z][_A-Za-z0-9]*\z/, pattern can be any legal perl regex.

After you successfully match the regex with a string, you can refer to the matched value with the %+ hash. In the example it is the $+{name} value of the hash that contains the matched value.

To created named backreference to a named capture buffer, you can use the \k<name> syntax. You could for example do the following:

(?<name>\w+) \k<name>

This would match with hello hello for example.

The best thing about this new feature is that it helps with documenting your program if you use meaningful names in your regexes.

I started using the Microsoft Natural Ergonomic Keyboard 4000 keyboard about two weeks ago. At first I didn't use the strange wrist thing. It looks strange and lift my wrists a lot.

Today I attached the wrist support thing and at once my typing is becoming faster and better. It looks like that thing makes the angle better for typing the keys. I've only tried this for a few minutes know. I will try it a little longer to find out how this works. It seems nice enough.

View archived entries