Archive for February, 2005

Cocoa, Objective-C, and Perl

Monday, February 28th, 2005

All computer languages have their advantages and disadvantages. Perl is a great language for parsing and massaging text. If you are writing a Cocoa, Objective-C program, and you need to parse and massage text, you may want to hand that task off to some Perl code. This can be easily done. OS X comes with Perl installed, and you can have a Perl program as one of the resources in your Cocoa application. Following is some sample Objective-C code and Perl code that illustrates how to launch a Perl program that will write a temporary file that can then be read by your Objective-C code:

Here is the Objective-C code to set up and launch the perl program:

// define your temp file
FILE *fp;
NSMutableString *tmpPath = [[NSMutableString alloc] initWithString:@””];
[tmpPath setString:@”tempfilepath/tempfilename”];

// this is the path to your perl program
plPath = [thisBundle pathForResource:@”perlpgm” ofType:@”pl”];

// array to pass arguments to your perl program
NSMutableArray *argArray = [[NSMutableArray alloc] initWithObjects:plPath,tmpPath,nil];
// launch the perl program & wait for it to finish
[[NSTask launchedTaskWithLaunchPath:[NSString stringWithFormat:@”/usr/bin/nice”] arguments:argArray] waitUntilExit];

// open the temp file that has been written by the perl program
fp = fopen([tmpPath UTF8String], “r”);

// process the contents of the temp file here

fclose(fp);

// remove the temp file when you’re done
NSFileManager *manager = [NSFileManager defaultManager]
[manager removeFileAtPath:tmpPath handler:nil];

And here is what the code in your perl program will look like:

$tempFile = shift;
open ( TMPFILE, “$tempFile” ) || print STDERR “problem opening $tempFile\\n”;

# do your text massaging here and write the temp file.

close TMPFILE;

Of course you can also use STDIN and STDOUT if you don’t want to write a temporary file.

multiple causes of error

Saturday, February 26th, 2005

In my 35+ years of programming computers of all kinds, using many different computer languages, and developing many different kinds of applications, I have noticed a recurring phenomenon that, I believe, is a universal principle of the human psyche, which is to say, of reality.

When I’ve finished the code for a particular feature in a program, I always run the program to test it and make sure it works like it should. But something is wrong. Some error occurs. So I go back and pore over the code until I discover the error in logic that is obviously causing the problem. Then I run it again, and, lo and behold, the exact same error occurs. How can this be? I know for a certainty that the thing I fixed would cause this error, and I have fixed it, and the error continues to happen. Same error, different source of error. So I perform the same process over again. This happens so frequently, I have come to expect it. There is almost always another, completely unrelated, glitch in the logic of the program, that causes the exact same error. Often there will even be one or two more such completely unrelated bugs, all causing the same result.

This same circumstance arises in our lives. We have some recurring problem that keeps coming up. So, we get therapy or read a self-help book, or just engage in self examination, and Eureka! We discover what it is we are doing that is making this happen over and over. We change, we fix it, and we feel better. And it doesn’t make any difference. The problem, whatever it is, doesn’t go away. It continues to plague us. When this happens, it means there is some other, completely unrelated, twist in our soul that must also be untangled.

The universe is constructed of bits that can be either zero or one. At least that’s what Marvin Minsky believes. But it is an idea as old as the hills, that runs through all science and religion. Positive and negative, good and evil, male and female, yin and yang. The universe is composed of a duality of opposites, no matter what you call them. This means that the process of programming a computer, which is ultimately a manipulation of bits of information that can be either 1 or 0, is a mini version of the process of Creation itself. That’s why geeks find it so addictive. There is a God-like feeling to it.

Unfortunately, the lessons learned from programming computers don’t seem to make computer programmers any better at solving their own problems than anyone else. This is either because they don’t apply their skills to other aspects of their life, or because they do, but that only fixes one cause. There is something else being overlooked.

when good disks go bad

Saturday, February 26th, 2005

Recently the internal hard drive on my 3 year old iMac started getting trippy. I had to reboot more often, and when I did, the iMac wouldn’t reboot until I did a disk repair on the drive. The only way to do this, of course, is to boot from the original OS X install cd, run the Disk Utility, do a repair, and then restart. In order to do this I had to boot while holding down the mouse button so as to get the cd drive to open up, insert the OS X cd, shut down using the power button, and boot up again holding down the ‘C’ key so as to boot from the cd. I had to do this every time. This made me very nervous since I have over 30 gigabytes of applications and data on my hard drive that I didn’t want to lose. I would imagine that this is a problem that many iMac owners are already having, or will be having in the future, since hard drives have a finite lifespan.

So, here’s what I did to fix this problem. I have an external firewire hard drive, so I decided that the thing to do was to somehow clone my internal hard drive onto the external drive, and change the boot drive to the external drive. Here are the steps:

1. Download this most excellent utility Carbon Copy Cloner by Mike Bombich. This is a donation-ware program (uncrippled shareware) and it’s very very good. If you use it, send Mike a donation.

2. Make sure there is enough space on the external drive to contain a clone of the internal drive.

3. Set the preferences in Carbon Copy Cloner to “Repair permissions before cloning” and “Make disk bootable”. You may also want to check “Delete directories before overwriting”.

4. Set the source disk to your internal drive and the target disk to your external drive.

5. Click on the little padlock icon and enter your administrative password, and then click on the Clone button.

6. Carbon Copy Cloner will then create a carbon copy of your internal drive onto your external drive, without disturbing any other data you happen to have on the external drive.

7. Once the disk has been cloned, restart while holding down the option key. This will bring up a dialog that allows you to specify the startup disk. Choose the external drive.

That’s it. You now have a new startup drive and all of your applications and data have been saved. Once I had done this, after checking a bunch of things to make sure everything worked the way it should, I erased the internal hard drive, and have been using it for backups. It has not given me any trouble since I erased it. Another benefit of the switch is that my external firewire drive is twice as fast as the internal drive, so applications load much faster, and things in general are snappier.