HHVM & Hack: The painless way to get started

So we have heard about the new PHP Virtual Machine from Facebook – HHVM and the static typing flavoured new programming language called “Hack” that comes with HHVM. If you haven’t already, check out hhvm.com and hacklang.org for more details.

Now, if you are on a Linux box, setting up HHVM would seem a little less complicated. But if you are on Windows or Mac OS X, the hell breaks lose. HHVM doesn’t have any support for Windows yet. You can manually compile it on OS X or perhaps use a package manager like Homebrew (which would automate the compilation for you). However, because of some bugs in HHVM, the setup for HHVM 2.4.2 has been failing (as I found out from the homebrew repo pointing to the hhvm bugs) on homebrew. I did have an older version installed but I wanted to go cutting edge. So, I needed a Linux box. If you are on Windows, this is a solution for you as well. I setup a Ubuntu VM to install the latest HHVM. I could just download a 64 bit ISO and install inside VirtualBox but I preferred Vagrant since it’s just easier to setup and use. I am going to walk you through the setup now.

Step – 1: Get Vagrant

Head over to: http://www.vagrantup.com/ and follow the instructions for your OS. It is quite simple. You would need to have VirtualBox installed for Vagrant to work.

Step – 2: Installing and launching the Ubuntu VM

Create a directory somewhere and cd into it.

Let’s create a vagrant configuration file:

This would create a file named “Vagrantfile” which is a configuration file written in Ruby.

Let’s add a box, a box file is basically a VM image and configuration packed in one file. We shall download one of the default box, Ubuntu 12.04 64bit which we would call “precise64″. “Precise Pangolin” was the nickname of Ubuntu 12.04 if you didn’t know. And please note HHVM pre built packages are available for Ubuntu 12.04 64bit, so we can’t go for 32bit here.

(PS: If you have a bad internet connection, download the file using a download manager and then use relative path like: “vagrant box add precise64 ~/Downloads/precise64.box” – that would work as well :D )

Now let’s make sure that we told Vagrant to use the newly created “precise64″ box as the base of our VM. My Vagrantfile roughly looks like this:

Now, let’s get the VM up:

This should setup the Ubuntu VM and we’re ready for installing the HHVM now :D

Step – 3: Setting up HHVM

SSH into the box:

You shall be SSH’d into the linux terminal. So you can play around :)

Now, we are going to install HHVM from the official repo. Type the following commands one after one to install. Explanation of the commands are available in comments:

If everything goes right, we should have a working HHVM installation!

(The above instructions to setup HHVM on Ubuntu 12.04 were taken and very slightly modified from: https://github.com/facebook/hhvm/wiki/Prebuilt-packages-on-ubuntu-12.04)

Step – 4: Saying Hello!

First, let’s see if HHVM was installed correctly.

So it was installed correctly.

Let’s try some Hack:

Output:

Now, feel free to try out HHVM and Hack along with the many cool things they can do.

Some useful resources:

A sample site: https://github.com/hhvm/hack-example-site

The PHP Manual with Hack references: http://docs.hhvm.com/

Have something to say? Please leave your feedback in the comments section.

Copying (duplicating) MongoDB Documents (Rows)

The use case is simple – I have one row (I mean document). I need to make multiple copies of it. Say, I am building a blog app and I need to quickly generate a few posts on the database. Scripting is one way to do it, but if I need to do this only once, automation is a waste of time.

Thanks to mongodb’s shell and JS-like APIs. This is actually simple.

Connect to mongodb:

Select the database:

Find an entry on the “posts” collection:

Now let’s change the “_id”:

X is now a whole new object/document (since we changed the “_id”). We can alter other fields as well. Let’s store it:

You just copied one document into a new document. Cool?

The command line mongo client has readline and history support. So you can actually use the up arrow to repeat the commands quite easily.

Debugging MongoDB Connection Issues

I am working on a project where I connect to MongoDB from PHP using the PECL extension. Regardless of your stack, we might time to time face strange issues when our clients can’t connect to the mongodb server. In this post, I would document what I did in my case.

First test if the mongodb server can be connected to. Use the “mongo” shell client.

If the connection succeeds, then the server is definitely reachable. You have made some mistakes in configuring your client. A very common mistake is having a malformed connection URI. So double check if you typed in your host, port and other details properly in the connection string.

If the above fails, that means the server is not responding. To begin with, try restarting it.

If the server started well, try again. Still connection failed? OK, the server might be damaged. How about checking the log files?

Nothing interesting? Or do not understand the format? Well, then let’s try blindly repairing the server:

Your problem should be resolved by now. But if it doesn’t solve the issue, your problem is not a common one. Try Googling further or asking on StackOverflow or other programming forums.

Using PhpStorm from Command Line

I know the title is not so interesting because we all know PhpStorm can be launched from the command line just using the “pstorm” command. Like this:

But I love the word “storm” more than “pstorm”, so I did this in my .zshrc:

How do we open a directory in PhpStorm? Just like the usual:

Ah, that will open the current directory in PS :)

But what if the command line tool was not installed properly? What if, while upgrading, the tool broke? Or if like me, you install EAPs all the time to try out new features and the command line tool can’t find the EAP version’s path? Well, in this blog post, I will share the python script that is actually installed as the “pstorm” tool. I will also tell you what to modify in case it can’t find your PhpStorm. And of course, we will see how to install it.

So first, the python script. If it’s installed on your system, you can view the content:

Or to edit it with your preferred text editor (in my case vim):

If you don’t have the script installed, here’s mine:

If you know Python, feel free to read and understand what’s actually doing. Now, this script needs to know two things:

– The path to PhpStorm directory (where the executable is)
– The preferences directory

You should be able to find the first trying to locate where PhpStorm is installed. For the second, it’s actually platform dependent. On OS X, it’s inside the “Home” > “Library” > “Preferences” > “WebIde**” directory.

The preference directories are named with a zero appended to the major version of PhpStorm. So, for PS 7.x it’s WebIde70. For 6.x, it’s WebIde60.

Just configure the two variables in the script. Then copy it to your path, in my case, I have “~/.bin” added to my PATH. So I did this:

The last line makes it executable. Now open a new terminal and try the “pstorm” command.

Embed a Python REPL in your program

So you love the Django shell? The interactive prompt that has your Django stuff loaded and ready for some quick prototyping? You always wanted to embed something similar in your Python apps too? Well, it’s painlessly simple with the “code” module. It offers us the “InteractiveConsole” class which we can extend to quickly get a REPL which is basically the Python REPL with our customizations. The code is heavily documented. Going through it should be adequate to understand whats going on:

What did we do? We subclassed InteractiveConsole to create our own implementation. And then ran it :)

Test run outputs: