Building Gearman 0.27 on CentOS 6

CentOS 6 comes with an outdated version of Gearman, version 0.14 released July 2010. (Ubuntu ships the same version; I wonder if it’s because the gearman.org download page incorrectly lists 0.14 as the latest version of libgearman?) The latest version is currently 0.27, so I tried compiling it on a fresh install of CentOS. I came across this error:

checking for Boost headers version >= 1.39.0... no
configure: error: cannot find Boost headers version >= 1.39.0

I installed the boost-devel package and tried again… and got the exact same result. The version of boost-devel in CentOS 6 is 1.41, so everything should have been fine. A quick google search revealed some posts by people having trouble compiling Gearman on older CentOS versions because boost was out of date, but that shouldn’t apply here.

I finally found this bug report which revealed the real problem: I didn’t install g++ (found in the gcc-c++ package). Installing the gcc-c++ package fixed the issue. If you’re starting with a fresh CentOS 6 install, make sure to install the gcc and make packages, too.

Finally, if you want to rebuild the CentOS RPM with the newer upstream version, here are the changes I made to the .spec file. Beware: I haven’t tested the RPM or Gearman 0.27 at all yet, I just got it building. Update: I just posted a spec file for the gearman PECL extension, too.

Vim Text Objects

They say it takes 10 years to master something, whether it be programming, photography, etc. I think Vim is an exception to this rule. I’ve been using Vim longer than I can remember, but every once in a while I learn something new about it that makes me feel like I’ve just begun to scratch the surface.

Today it was Vim text objects. I use motions like dw (delete word) and ct) (change to ) character) all the time, but motions require that my cursor be in the right place when I issue the command. If I do a dw in the middle of a word, it will only delete from my cursor to the end of the word. With text objects, I can do a daw with my cursor anywhere in the word, and it will delete the whole word. Or sentence. Or paragraph. No more wasting keystrokes navigating to the beginning of the paragraph!

Even better, there are programming language-oriented text objects, and they search the entire line! So you can do ci' anywhere on the line, and Vim will find the first single-quoted string, delete the contents, and put you in insert mode. There are even text objects for HTML markup (at, a>)!

Go read the guide now. In the meantime, I have some new muscle memory to practice.

Nested Object Iteration in PHP

If you code in PHP, you’ve probably used a foreach loop.

$array = array(1, 2, 3);
foreach ($array as $value) {
    echo "$value\n";
}

// Prints:
// 1
// 2
// 3

Back in the Stone Age (i.e., before PHP 5), you could only use foreach to iterate over arrays. But as of PHP 5, you can use foreach to iterate over objects, too. By default, iterating over an object will return its visible properties.

$object = new StdClass();
$object->foo = 1;
$object->bar = 2;
$object->baz = 3;
foreach ($object as $value) {
    echo "$value\n";
}

// Prints:
// 1
// 2
// 3

You can control object iteration behavior by implementing the built-in Iterator interface. (Actually foreach looks to see if your object implements Traversable, which is extended by Iterator or IteratorAggregate, but I’m going to ignore IteratorAggregate for now.)

Implementing Iterator lets you implement more meaningful foreach behavior for certain list-like objects such as stacks, queues, friend lists, e-mail recipients, payment methods, etc. Making your objects iterable can lead to very nice looking code:

// Nice and clean
foreach ($friendList as $friend) {
    $friend->tellThemWhatIHadForLunch();
}

// Ugly
for ($i = 0; $i < $friendList->count(); $i++) {
    $friend = $friendList->get($i);
    $friend->tellThemWhatIHadForLunch();
}

There is one thing to be aware of — nested iteration over the same object does not work.

// SplFixedArray is a built-in class that implements Iterator
$array = new SplFixedArray(3); 
$array[0] = 1;
$array[1] = 2;
$array[2] = 3;
foreach ($array as $value) {
    foreach ($array as $innerValue) {
        echo "$value, $innerValue\n";
    }
}

// You might expect this to print
// 1, 1
// 1, 2
// 1, 3
// 2, 1
// 2, 2
// 2, 3
// 3, 1
// 3, 2
// 3, 3

// But it actually prints
// 1, 1
// 1, 2
// 1, 3

This is because your object only has one internal position. The outer foreach loop sets the internal position to the first item. Inside the loop, the internal position is reset to the first item, then the second, then the third and last item. When control returns to the outer loop, there is no next element (the internal position is still pointing at the last item), so the outer loop exits.

At first glance, this limitation seems straightforward to avoid — just don’t write nested loops that iterate over the same object. (If you really need the functionality, you can use a regular for loop and track the position manually with a counter variable.) But sometimes it might not be so obvious.

I found a case of this problem in Zend\Di last night. An outer loop in Zend\Di\Display\Console was iterating over a Zend\Di\DefinitionList object, and in the loop, a few methods were being called on the same Zend\Di\DefinitionList object.

foreach ($definitions as $def) {
    // ... Do some stuff with $def ...

    $definitions->foo();
    $definitions->bar();
}

No nested iteration there, at least at first glance. The problem was, the implementations of those methods did something like:

foreach ($this as $definition) {
    // ...
}

So there was nested iteration, but it was split across two different classes.

As in the trivial SplFixedArray example, above, the result was that the outer loop only iterated over the first item. So if you ever encounter a foreach that appears to exit prematurely, and there are no break statements in sight, check for nested iteration.

Note that this issue only exists for objects implementing Traversable. I tested nested iteration over primitive arrays and object properties, and both worked as expected in PHP 5.3.8. In those cases, PHP must be storing the position pointer on a per-foreach basis.

Jenkins, Ant, and Closure

At work we use Closure to minifiy our JavaScript at deploy-time. We also use Jenkins (we actually named it Leeroy Jenkins, because I work at IGN) to automatically test and merge changes from our integration branch into our production branch. I wanted to configure Jenkins to build our JavaScript files with Closure to ensure they’re syntactically valid before merging them into the production branch.

My first thought was to write a Python script, but it seemed lame to have our static repository depend on Python, even though it comes pre-installed on most Linux distributions. Plus, I didn’t want to have to add a bin directory to the static repository. (We have a separate git repository for static CSS, JavaScript, and image files to keep the main code repository lightweight.) Running all the JavaScript files through Closure is a simple enough task that I should be able to write a shell one-liner and just stick it in our build.xml Ant file, right?

Continue reading

Zend Framework 2 Beta 2 Released, APC 3.1.9 for Lucid

I wasn’t very productive over the holidays (I blame Valkyria Chronicles), but it’s the new year, and that means new posts!

I’ve been playing around with Zend Framework 2, both at work and at home. As I mentioned in my presentation at SF PHP, it’s not ready for production yet, but each beta release includes a few more refactored components. I’ve been trying to play with each component as it’s released so I have a good understanding of how all the components work and how they might fit together in an app. I hope to blog some examples and best practices in the coming weeks.

Zend Framework 2.0.0 beta 2 was released in late December. It included a refactored Zend\Cache component, which requires APC >= 3.1.6. Unfortunately, I’m running Ubuntu 10.04 LTS (Lucid), which ships with APC 3.1.3p1. I was tempted to just spin up a new Precise Pangolin AMI, and I will probably end up doing that eventually because ZF2 requires PHP >= 5.3.3 and Lucid ships with PHP 5.3.2. But for the short term, the ZF2 MVC components seem to run on PHP 5.3.2, so I just compiled the latest version of APC, 3.1.9, for Lucid. The backported package is available on my PPA if you’re interested. Hopefully I can hold out until April when Ubuntu 12.04 LTS (Precise) is released.