Rubénerd Blog :)

Friday 18th September 2009

Programming language work and nostalgia

Under The Bed

Photo above is of my bedroom in Singapore in 2004 shortly before we moved again. The reason why its there in a second!

I’m not at liberty to divulge what or for whom for the latter, but over the last two days I’ve been frantically working on two programs, one for an assignment. 3,900+ lines for the work, 720+ for the assignment, neither of which were particularly complex. Still, my eyes only just stopped protesting when I gave them some eye drops!

Unless you count XML or SQL I’ve never programmed concurrently in two different languages before, fortunately Java and Python are different enough that I wasn’t accidentally writing Python code and expecting javac to interpret it properly! One thing you do really start to appreciate when you do Python and Ruby is just how verbose (and to a large degree tedious) Java is. Having to write entire setter and getter methods to just return what is essentially primitive data, not having a brain dead simple way to read characters from prompts, the gloriously simple Ruby way for dealing with arrays, I could go on. I guess in Java’s favour for a learner like me it’s fairly consistent and the Javadocs are very thorough and relatively easy to follow.

It’s funny though how my brain has started thinking of programming languages as tools in which there’s a right one for a job. I could have used Automater on Mac OS X but when I needed to organise a bunch of files and rename them for both projects I wrote a quick Perl script. I never really learned much shell scripting, so for my Perl fills that roll, and it does a superb job. I can remember when I was learning Perl in high school, those were happier days so it always makes me feel warm and fuzzy. Acme! CPAN! Perl golf! Like I’ve said here, I feel as though my life peaked in 2004, it was a magical year.

Earlier in high school we did Visual Basic, but I’d sooner call my foot a toaster than dignifying it with the title of "programming language"! Well okay I did have fun with that too, I remember showing Ms Coupland some really stupid nonsensical applications that didn’t do anything.

The other funny thing is I while I did do some rudimentary QBasic and QPascal in primary school, the "programming language" I used the most when I was a kid were simple DOS BAT (batch) files. I remember writing silly text based games and at one point I even had a crappy flat file organiser for our CD collection which I lost interest in once I realised my dad was so impressed he wanted me to type all the CDs into it! I really wish I could find it.

The next things I want to get involved with in my own spare time if I ever get any again is to continue playing around with my second hand Sinclair Spectrum or even better giving some PDP-8 assembler a try.

I’m sure most of you have some nostalgic programming stories, anything fun or interesting you want to share?

Friday 21st August 2009

#SongsInCode

Rick James Super Freak

As if I needed one, but another reason why I love Twitter is the spontaneous memes with a half life shorter than the time it takes for the fizz in soft drink to dissipate when the bottle is opened. I prefer Solo. Well this paragraph rapidly degenerated into nonsense more quickly than most.

Today’s fun was coming up with expressing famous songs as code with the surprisingly descriptive #SongsInCode hashtag. @NickHodge by far had the best ones, but I tried my hand at a few too. I spaced them out to make them easier to read, but all these fitted into the 140 character Twitter limit when written on the one line!

if (destination == "San Francisco") {
   hair.wear(flowers);
}
4.times do
   puts "My"
end
puts "Sharona"
if ( headlight >= 1 ) {
   [driveTo : home];
}
%signs = ( "Wild", "Can't Take Home to Mum", "Kinky" );
if ( exists $signs{$girl} ) {
   print "That girl's a super freak!\n"
}
int Mambo = 5;
if ( yourself.takenLookAt() ) {
   me.accuse(allow);
}
for ( my $i = 1; $i < 13; $i++ ) {
   if ($i % 4 == 0) {
      print "$i o'clock rock!";
   } else {
      print "$i o'clock";
   }
}
try {
   makeMeGo("rehab");
} catch(WinehouseRefusalException e) {
   System.out.println("no no no!");
}
Word thing = "Bird";
System.out.println ("The " + thing + " is the "
   + thing.getClass().getName() );

While all those were fun, I think the last was my crowning achievement :).

Saturday 18th April 2009

Restoring posts since February finally done!

My workspace from this evening. Now all I have open is this browser window :)
My workspace from this evening. Now all I have open is this browser window :)

Well after several long hours of debugging, cursing and several tall cups of coffee, my Perl script to import static pages from Google’s cache, convert them to a WordPress WXR/RSS file and import here worked! This means all the blog posts from February 2009 to April 2009 that weren’t part of my last good backup are now available again.

Had it not been for Google’s cache, over 120 posts would have been lost.

I’ve started a cron job to make automatic backups every evening in both WXR/RSS and SQL formats and send them to a secret email address so this mess never happens again. I’ll also be looking into checksumming/hashing said files before and after their transfer so I don’t end up with the same invalid, messed up export files I had to deal with and piece back together this time around.

Aside from the changed webhost and URI, everything is now back to the way it was before. The only things missing are comments; if you left a comment on a post over the last two months feel free to write another if you could be bothered.

Next step is to finally implement my new theme I’ve been kicking around for a few months. As I said in the last post I’ve tried my best to set up correct redirects for search engines and people subscribed, as well as changing the URIs on all the posts here to speed things up. If you see something amiss, feel free to leave a comment.

UPDATE: If you see repeated posts in Google Reader, Bloglines or other blog readers, I’m sorry for bombarding you! I attempted to maintain the permalinks so this wouldn’t happen, but clearly Google Reader and Bloglines still thought they were new posts.

UPDATE: It seems a couple of posts are still missing because they were uploaded too recently to have been put into Google’s cache. Tomorrow morning I’ll go to trusty Google Reader and copy them back here. Google has been a real pal over these last few days!

Wednesday 24th December 2008

Using env in shebang scripting language lines

Directly referencing your interpreter? Crazy...
Directly referencing your interpreter? Crazy…

I haven’t written any geeky programming technical posts for a long time. Christmas Eve seems like just as good a time as any. Funny how Perl programming specifically always reminds me of Christmas because I got my first proper job after high school in 2004 writing Perl/MySQL code around Christmas time so I could buy people presents. Happiest time of my life then because I had purpose, direction and optimism.

This morning I’m talking about using env in the shebang line of scripting language source files (FreeBSD man reference). Quite frankly I’m surprised that as of 2008 the majoraty of files I download and look through still reference hard links to the interpreters such as the examples below:

#!/usr/bin/perl -w
#!/usr/local/bin/ruby

The problem with hard/specific referencing is that you can’t assume your system will have a filesystem configured in the same way as another. While us users of Unix-like systems are fortunate that our said systems have so much in common, there are still enough subtle differences between Unix-like OSs and even distributions of the same OS to cause problems.

For example, most GNU/Linux systems place all non system critical files in the /usr/bin directory. As a FreeBSD user I shivver in terror at the mere thought of this; on the BSDs we’re even more specific and seperate non system critical files that come as part of the core system in /usr/bin, and files we later install ourselves in /usr/local/bin or /usr/local/pkg depending on our package manager. Much stricter, cleaner and easier to maintain… but that’s for another post.

The solution to this is to call our scripting language using env which checks your system for the desired interpreter and executes. This means Linux users can share with BSD users can share with Mac OS X users etc without worrying about how said systems are configured. You can even pass regular options, very nice.

#!/usr/bin/env ruby -w

Using env to reference basic sh shell scripts would be overkill, but for languages such as bash, Ruby, Python, Perl etc it just makes sense. Sure you could just assume all your clients and servers will be GNU/Linux folk, and perhaps you’d be right… for now. I’m all for future proofing.

Monday 04th August 2008

Western Digital MyBook DHCP adventure

Before I go any further, I’d like to warn you that this post about using FreeBSD to assign an address to a Western Digital MyBook drive is unnecessarily verbose. See, I could have just as easily said "this post is long".

Writing about theoretical uses for software is one thing, but figuring out how to apply software to a problem practically, and actually getting it work, is one of the greatest feelings in the world alongside writing a fantastic piece of software yourself to solve a problem. It makes me feel useful and gives me a rush quite unlike anything else.

And he’s available ladies! Unbelievable though it may seem! Come on! He also makes a mean Earl Grey latte. Any bidders? Anyone?

Case in point, my sister and I had a problem in our new house in Adelaide. Before I left Singapore, I bought a gigabit ethernet Western Digital MyBook 1GB World Edition NAS drive and loaded it up with all our media: movies, shows, music, podcasts, BSD disk images, ebooks. By using a simple network drive, we spared ourselves having to bring a separate computer down with us.

Our makeshift home network consists of each of our laptops and the aformentioned network drive which are all connected through short Cat-6 ethernet cables to a 5 port gigabit ethernet switch. To compensate for the lack of a router, I assigned my sister’s MacBook and my MacBook Pro static IP addresses and I knew the network drive could also be assigned one. Very cool.

Our very fancy new home network
Our very fancy new home network.

Now here’s the kicker: for some reason in their infinite wisdom Western Digital decided not to ship the MyBook with a default IP address. This means it requires a DHCP server (Wikipedia link) to initally provide it with an address. I realised that without a router with a built in DHCP server, this network drive wouldn’t get an address, and therefore there’d be no way for me to log into it to assign it a static IP! Catch 22, chicken and the egg, a Bruce Schneier fact, call it what you will.

So within a few minutes I had configured a home network with two laptops and an inaccessible, address-less network drive. It seemed not having a router with a built in DHCP server would make this setup impossible. A quick Perl script determined that indeed only the two laptops were in our subdomain range:

#!/usr/bin/env perl -w

for ($i = 1; $i < 255; $i++) {
  system "ping -a -c 2 192.168.1.$i";
}

print "Western Digital engineers love DHCP it seems\n"

At this point I had to make a tough decision. To access our media network drive I could either be patient and wait a week until we got our new broadband modem with a built in router, or I could be impatient. Grilled cheese sandwich. Grilled cheese sandwich?

I decided to do the latter and give it what it wanted: a DHCP server! In place of a router, I would configure a one-time use FreeBSD virtual machine with a software DHCP server which would assign the address to the network drive, so I could log into it remotely and assign it a static IP.

My FreeBSD virtual machine showing the DHCP server coming online
My FreeBSD virtual machine showing the DHCP server coming online

After setting up a generic VMware Fusion virtual machine on my MacBook Pro, I mounted a FreeBSD 7.0-RELEASE ISO I had on already had on my hard drive and installed it with all the default options. I skipped configuring everything in the system installer except than the network card. I declined to use DHCP when it asked, and assigned it a static IP.

After rebooting the VM, I logged in as root and installed the isc-dhcp3-server package from ports.

I didn’t bother adding anything fancy to the /usr/local/etc/dhcpd.conf other than defining the pool of addresses and required options:

option domain-name "chuckpeddle";
option subnet-mask 255.255.255.0;

default-lease-time 3600;
max-lease-time 86400;
ddns-update-style none;

subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.120 192.168.1.129;
}

Next, to check what addresses our makeshift DHCP server has assigned, I created the default dhcpd.leases file:

# touch /var/db/dhcpd.leases

Finally, I enabled the dhcpd daemon in /etc/rc.conf and told it what card I wanted FreeBSD to listen for DHCP requests on. On VMware Fusion 1.1.3, the network card in FreeBSD is le0, but you can always check to make sure by using ifconfig -a

dhcpd_enable="YES"
dhcpd_ifaces="le0"

The moment of truth had arrived! I turned off my Western Digital MyBook NAS drive, rebooted the FreeBSD virtual machine, then turned the network drive back on again. After a minute of waiting, sure enough the /var/db/dhcpd.leases file reported that it had assigned an address…

lease 192.168.1.129 {
  starts 6 2008/08/02 14:37:12;
  ends 6 2008/08/02 15:37:12;
  binding state active;
  next binding state free;
  [...]
  client-hostname "RubenerdNAS";
}

… and automagically the Finder on my MacBook Pro reported it had found a new samba share (with it’s irreverent sense of humour)! My network drive had been resurrected from the dead!

My Western Digital MyBook in the Finder
My Western Digital MyBook in the Finder

Finally, after all of that I was able to go to my web browser and log into the damned network drive’s configuration page and change the IP address to a static one.

I still can’t help but wonder why Western Digital decided not to include a default address.

Western Digital NAS config screen, and the FreeBSD DHCP virtual machine
Western Digital NAS config screen, and the FreeBSD DHCP virtual machine

And now if you’d excuse me, I’m off to watch an episode of Cranky Geeks. From my network drive. PHEW!

Saturday 24th May 2008

Just ordered a Yubikey

YubikeyAfter listening to a recent episode of Security Now I’ve gone ahead and purchased myself a Yubikey!

The Yubikey is a phenomenal new device that’s smaller than most memory keys that when plugged into a USB port and the loan button on the case is pressed, a one time password is generated and entered. It does this on any HID enabled operating system including my beloved Mac OS X and FreeBSD without extra drivers because it shows itself as a regular USB keyboard. It’s so beautifully simple!

The best part is that the API is open and accessible from a number of different programming languages such as Ruby, PHP and Python (not sure about Perl just yet).

In the coming weeks I’m going to try to implement my Yubikey into my Ruby CMS. I’m really excited!

Thursday 22nd May 2008

Latest computer book haul

Having received payment for my latest one off project today, I celebrated by going down to Wheelock Place and splurging on a Starbucks Venti Dulce de Leche from next door and buying some computer books I’ve been dying to get. I’m a wild guy you see.

Happiness is a stack of new interesting computer books!
Happiness is a stack of new interesting computer books!

I find that I learn new skills much faster if I’m given examples and real world applications of technologies rather than just the usual “an array is a collection of objects yada yada” theory. The O’Reilly Cookbooks are absolutely fantastic for this, what irritates me is that I only just discovered them recently when I had to learn Python in a hurry for an assignment. I learned more from that one book than many hours sifting through tutorial pages and the dry slides from the uni.

Starbucks Dulce de LecheAs for the FreeBSD book, heck I just wanted to see how it works! Perhaps a little over my head right now, but we’ll be looking at the Linux kernel at some point so this could be an interesting side study for comparison.

From the blurbs:

The Design and Implementation of the FreeBSD Operating System
As in earlier Addison-Wesley books on the UNIX-based BSD operating system, Kirk McKusick and George Neville-Neil deliver here the most comprehensive, up-to-date, and authoritative technical information on the internal structure of open source FreeBSD.
Perl Cookbook
Find a Perl programmer, and you’ll find a copy of Perl Cookbook nearby. Perl Cookbook is a comprehensive collection of problems, solutions, and practical examples for anyone programming in Perl. The book contains hundreds of rigorously reviewed Perl “recipes” and thousands of examples ranging from brief one-liners to complete applications.
Ruby Cookbook
The Ruby Cookbook is the most comprehensive problem-solving guide to today’s hottest programming language. It gives you hundreds of solutions to real-world problems, with clear explanations and thousands of lines of code you can use in your own projects. From data structures and algorithms, to integration with cutting-edge technologies, the Ruby Cookbook has something for every programmer.

And now I’m off to bed. 00:07, that’s pretty early for me!

Sunday 14th January 2007

Haruhi Suzumiya dancing on FreeBSD?

Today was pretty amazing. Why? I ate a small mango after brushing my teeth and was not bothered by the nauseating spearmint infused citrus flavour.

Actually something big did happen for me today and it’s something that I’ve been wanting to do for ages. I installed from scratch FreeBSD on several PCs and without looking at any of the documentation for the first time. I’m dangerous!

The BSD Beastie   haruhisign.gif

In a manner similar to how DarkMirror in Singapore talks about learning Japanese, my knowledge of FreeBSD didn’t consciously happen, it evolved and eventually clicked. Like most people starting out in the FOSS world after living on Mac OS X and… heaven forbid… Windows, I was somewhat confused by the more intricate details of compiling kernels with optimisation flags and updating port trees with CVS; but after a week of informal study I pulled my old 200MHz Pentium MMX machine out and installed FreeBSD without looking at any documentation. Without realising it until after I finished, the process of installing and configuring has become second nature.

I really am impressed with the quality of the BSDs and the open source community’s efforts. Xorg + KDE 3.5 or Xfce 4.2 on a FreeBSD 6.1 box with the correct optimisations works flawlessly even on hardware that would struggle with Windows 98! Suddenly all the old machines that litter my expat family’s house have uses. Heck I’m even looking at my old Amiga in a funny way now…

I feel such an overwhelming sense of power now than I did before in a way that proprietary operating systems never really allow. Open source rocks! If I don’t like my X window manager, my desktop environment, my CLI text editor, my shell, my file browsers, my titlebar widgets or even my daemon services that manage SQL or the web, I can just change them or swap them for something else. A company doesn’t dictate what software has to be installed, I do. And if something doesn’t exist or I need to do something mundane, a quick keyboard lashing session later I’ve got Perl doing it for me.

The BSD Beastie   haruhisign.gif

Now I could have continued my exploration of Linux or just continued to hack away at Mac OS X, but I really do appreciate the strict conventions that the BSDs follow and the unwritten mantra that “if something works it’s good” should actually be “if it’s good it will work”. FreeBSD, NetBSD (and from what I can tell from my currently limited experience, OpenBSD) are elegant, fast, very well documented and extremely robust.

It’s got to the stage now where I’m even considering dual booting my MacBook Pro with FreeBSD and Mac OS X just to be able to use this stuff more and more in my day to day life.

For posterity (and because I think they’re cool) I’ve posted some screenshots of some of the boxes I have happily running now. You can see the Mac OS X influence in my thinking with both the KDE and Xfce panels set up to look like the Dock and the permanent menu bar ;).

Here’s Haruhi happily living in KDE 3.5/FreeBSD on my old HP box, (with the cursor unfortunately positioned on her face!!):

Haruhi on KDE + FreeBSD

And here she is again in Xfce/FreeBSD on my 200MHz (with MMX… wow!) 1997 Sim Lim Square box:

Haruhi on Xfce + FreeBSD

I’ve also configured an old 133MHz Pentium box I picked up for peanuts to run as a dedicated firewall and local intranet webserver to serve up the Schade family wiki, kinda like a digital refrigerator door! Of course I couldn’t put Haruhi on this because I don’t want or need X on this machine. Ah well, can’t win them all!

My next weekend project is to create a Perl script to download portsnap updates so they’re residing on a local hard drive, then configure portsnap on each machine to look up the local server instead of retrieving the same image from the interent four times! Unless anyone knows of a port that already does something similar to this?

The SOS Stack!

Thursday 21st December 2006

Rubenerd Show 193 (Thu 21/Dec/2006)

The freaky changes episode!

"Evolution" of singers (Britney Spears going weird, Tony Bennett hasn’t changed!) learning langauges (German, Japanese, MandarinPerl!), software feature review (iTunes 7 cover art downloading) and Paulaner on why Germans are always on time!

Download MP3 ↓ 10:00 minutes, 4.6MiB

You can also stream it and view its Internet Archive page.

Dedicated to my groovy late mum Debra Schade.