Archive for May, 2005

Stupid Perl Debugger Tricks

I’ve been playing with Python alot lately in my spare time, but I still use mostly Perl at work. One of the handy things about Python is the interactive mode; I like it so much I even cloned it for Perl some time ago. Even without my Perlthon script, you can get a quick approximation in perl using the perl debugger and a command-line script:

perl -de1

(That’s a one, not an el.) The above will invoke perl with the debugger (-d), debugging a very simple script (-e1, which is to say -e '1;'). Once the debugger starts, you can just type perl statements, and can use x <expr> to inspect values.

Whichever way you play with interactive Perl, testing regular expressions can be a pain. It’s not too bad under Python, given Python’s use of match objects:

import re'regex', 'string').group(0)

The second line runs the regex against the string, and prints the entire match. If your regex doesn’t match at all, you get an error, but that’s fine (and self explanatory). If your regex doesn’t perform as expected, repeated attempts make it easy to triangulate. If you have Python’s readline module installed, you can just hit UpArrow after the test, tweak the regex, lather, rinse, repeat. I wanted the same flexibility with interactive Perl; it turns out to be trivial:

# perl debugger version
x ('string' =~ /regex/, $&)[-1]

# perlthon version
('string' =~ /regex/, $&)[-1];

The regex match operation will return a list of matched groups, which can be handy at times. For testing a complicated regex, I often just want to see the whole match to be sure I’m getting what I expect. The array notation accomplishes this nicely.

HOWTO: Mount an OS X Hard Drive from a Ubuntu LiveCD

One of the selling points of ‘LiveCD’ Linux distributions is that they allow you to try out Linux on your computer with out touching anything on your hard drive. Sometimes, however, you you want to do exactly that. While playing with a Ubuntu LiveCD on my Powerbook, I decided that I wanted to do exactly that. These are the steps that I followed, after some Googling led me to this thread on the Ubuntu forums.

Please Note: By default, the Ubuntu LiveCD will not touch your hard drive. This is for your safety. The steps below allow you read and write to your hard drive’s OS X partition while running Ubuntu from the Live CD. If this makes you in any way nervous, don’t do it. If you don’t know why it should make you nervous, don’t do it. I make no warranties. You could lose all your data, your job, or your hair. In fact, you should just stop reading now, and go watch television.

Still with me? Okay. You were warned. From the Ubuntu Applications menu, choose Utilities, and therein choose Terminal (aside from the use of a menu instead of Finder, it’s just like OS X). Once in terminal, you need to create a ‘mount point’, which is just an empty directory where you want to be able to access your OS X partition. The traditional place for this is in /mnt:

sudo mkdir /mnt/macosx

Just as in OS X, sudo will make your command run as root. When running from the LiveCD, you will not be prompted for a password, so make sure you’re sure about your commands before pressing Return.

Next, we need to find out what hard drive partition your OS X data is on. OS X normally creates a number of small partitions for its own use. To check your drive, you can use parted. Another Dire Warning: parted‘s raison-d’etre is viewing and changing disk partitions. It can and will destroy your harddrive. If you’re nervous, don’t do it. Here’s my entire parted session:

ubuntu@ubuntu:/usr$ sudo /sbin/parted
GNU Parted 1.6.20 with HFS shrink patch 16
Copyright (C) 1998 - 2004 Free Software Foundation, Inc.
This program is free software, covered by the GNU General Public License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more

Using /dev/hda
(parted) print
Disk geometry for /dev/hda: 0.000-38154.375 megabytes
Disk label type: mac
Minor    Start       End     Filesystem  Name                  Flags
1          0.000      0.031              Apple
2          0.031      0.058              Macintosh
3          0.059      0.085              Macintosh
4          0.086      0.113              Macintosh
5          0.113      0.140              Macintosh
6          0.141      0.390              Macintosh
7          0.391      0.640              Macintosh
8          0.641      0.890              Patch Partition
10       128.891  33050.109  hfs+        Apple_HFS_Untitled_2
(parted) quit
Information: Don't forget to update /etc/fstab, if necessary.

First, I invoked it with sudo /sbin/parted. Under Ubuntu Live, only root can access /dev/hda, which is your harddrive. Note the line Using /dev/hda. If you see something else, you can try select /dev/hda, but you probably forgot the sudo, or else you have a system which is different enough from mine that you should just type quit now and seek another source of information.

At the first prompt ((parted)), I typed print which printed the partition table below. As you can see, my OS X partion is number 10. So I type quit and return to the bash prompt.

Now it’s time to mount the drive. Here’s the command I used, using the mount point I created earlier and the partition number I learned from parted:

sudo mount -t hfsplus /dev/hda10 /mnt/macosx

If you get no ouput, success. Otherwise, something went wrong, and you should quit now or seek other counsel. Assuming it worked, you should be able to access your OS X partition thusly:

cd /mnt/macosx

Your OS X partition is now mounted under /mnt/macosx. So for example, my OS X home directory, which is normally /Users/jclark, is now /mnt/macosx/Users/jclark. Should you want to write any of the directories in the mounted partition (don’t do it!), you’ll need to run your commands with sudo.

Final word: I don’t know for sure how Ubuntu will unmount the partition when I shut it down. It should be fine, but an ounce of prevention is worth a pound of reformatting. To unmount your partition, use umount (cd out of the /mnt/macosx directory tree first):

sudo umount /dev/hda10

You can double check your work by running mount with no arguments to see a list of all mounted partitions. After the umount, /dev/hda10 (or your equivalent) should not be listed.


So, I have this thing for playing with new operating systems. I’ve built bunches of Linux boxes over the years, which I never really use for much, especially now that I have two Macs running OS X. I even tried Linux From Scratch once.

With the recent release of version 5.04 of Ubuntu Linux, I decided to play around. I was pleased to see they offer a Live CD (bootable Linux Desktop on a CD, no install needed) for PowerPC (Mac) as well as Intel x86 (PC) machines. I decided to try booting it up on my 15″ Ti Powerbook. It worked… I’m posting this from Firefox under Ubuntu.

Ubuntu Screenshot

I haven’t exactly stretched its limits, but it seems to work very nicely. Firefox is present, as is Evolution for mail (haven’t tried it, but folks seem to like it), Gaim for Instant Messaging, The Gimp for image editing, and OpenOffice for office-style applications. It feels a little slow, but it is running from the CD and ramdisk (memory). Slowdowns seem to coincide with CD access, so I’m guessing a full install would be snappier.

I’m not contemplating switching away from OS X, but it’s nice to see I have a viable option for my existing hardware if I did want to. Also, Sherri’s Windows XP laptop is getting slower and slower, and desparately needs a wipe and fresh XP install. However, I don’t have XP install media, since the laptop only came with a restore disk (lost at the moment), and besides, the default install wasn’t very clean. Instead of spending $129 for XP, I may have her give Ubuntu a try for a few days. I’m sure an actual installation would fly compared to her XP install.

Python unittest Greenbar Without the GUI

Lately I’ve been learning Python (again), and trying out Unit Testing using Python’s unittest module. In many unit testing tools, each time the test suite is run, a progress bar is shown, which is green as long as the tests pass, but turns red when a test fails. Passing your tests is called “getting a greenbar,” and is the mark of success.

I’ve been working on my Powerbook, using the default Python install. I work with two terminal windows open; one running Emacs, and the other to run my tests in. The unittest module provides a command line method of running all tests, with output appearing on the terminal. I wanted to have a greenbar/redbar indicator, but none is present.

There is a GUI version, unittestgui, available which uses tkinter, but doesn’t ship with OS X. Google turned up a Cocoa app for OS X for running Python unit tests, but it hasn’t been updated in a few years, and just crashes on my Powerbook.

Since I prefer to develop in a terminal session anyway, I decided I wanted a way to run my unit tests from the shell, and see the red/green effect right in the terminal. Thus I created utest, a Bash script that runs a unittest, and changes the display text color using ANSI escape codes based on the output of the test. Since unittest prints all testing output to STDERR, I redirect it to STDOUT and pipe the the whole shooting match into perl one-liner that parrots eveything, while setting colors as appropriate. Output starts green, but turns red if the output contains FAIL or ERROR. It also unbuffers standard out, so the results appear correctly.

Here it is in action, when all tests pass:

utest with passed tests

And here it is, with a failing test:

utest with failed test

If you can’t see these images, you’re probably using IE, so don’t come crying to me. You know what to do. If you’re wondering about my tabbed terminal, it’s iTerm.