Perl and Binary Protocols

Today I wanted to clean up a problem that has been sitting around for a long time. One of my price servers has had C++ and Java clients, but no Perl client. I have been holding off on the Perl client for a while because the API uses binary data for the price structures and I was worried about the complexity of dealing with that in Perl. Wow, was I ever wrong.

I took a similar API that had been written for me, and changed all the aspects of how the requests were sent, and how the data was returned. But once I had the data, I was still at a loss as to how to deal with it effectively. Then I came across pack() and unpack() in my Googling. What an incredible simplification this made for me.

I could simply point the unpack() to the start of the individual components in the fixed-length binary record and it handled all the details for me. Amazing. I was thinking that it was going to take me several iterations to get the byte order right and make sure all the numbers got passed back properly, but in actuality it took me a few minutes to get the first data type going and then a few seconds to do the others. Really quite an incredible feature.

I was then able to re-write a web page which was a PHP page shelling out to a Java program to hit the price server with a simple cgi perl script and it'll run much faster with much less load on the web server. Really outstanding. I am beginning to like Perl. It's got a lot of power that isn't obvious until you start to really exercise it.