Posts tagged with "perl"


Using TextExpander with Perl? Hell yes!

After hearing about it on my beloved 5by5 Back to Work, I finally started using TextExpander. Today, I realised it can be used with my lifehacking language of choice.

Way of the future

TextExpander lets you assign blocks of text (or even images) to a shortcut you assign. For example, when I type ;nc it expands out to all my ncurses boilerplate. ;sig expands to my name, address, email, EARLs and phone numbers. You could leave it at that, and be crazy productive.

But you don't have to stop there. If you create a new shortcut and choose choose "Context: Shell Script" from the top of the editing box, you can write shell scripts to process stuff. I ignored it initially, largely for the same reason I eschew (gesundheit) most Western fast food. I can eat it, but I prefer other things.

Earning some Siracusa cred

On a hunch this morning, I decided to test the limits of this "Context: Shell Script" box and enter some Perl (which of course I inserted with its own TextExpander shortcut!):

#!/usr/local/bin/perl [...]
print("Hello, world");

When I typed my ;test shortcut, Hello was inserted in it's place.

Oh. My. SCIENCE.

That's right, TextExpander took the output from this Perl script, and used it in the substitution. Think of the possibilities. No, seriously, think of them.

I've used Perl scripts, a Dock shortcut and the Mac pasteboard to automate and send results to where I want since 2004. Now I'm going to go through them all and see how I can make TextExpander shortcuts for all of them.


Non-destructive Perl regex substitutions

I've been a Perl Monk in training for years, but I only just realised this.

Say you want to print an original string, and a modified version with a case-insensitive substitution. This below will output "shimapan shima-melonpan", just because I can.

my $first = "shimapan"
my $second = $first;
$second =~ s/pan/-melonpan/i;
print("$first $second\n");

You can use "r" instead to non-destructively substitute, with the same result.

my $first = "shimapan"
my $second = $first =~ s/pan/melonpan/ir;
print("$first $second\n");

I generally avoid Perl Golf-isms for the same reasosn Dagolden does, but in this case I find it does remove a little redundancy, and is just as readable.

Thanks to @damncabbage for getting me to finally post this!


Happy 25th birthday, Perl!

I use half a dozen languages for work and fun, but when I need to quickly get stuff done, Perl still does the job. I couldn't imagine my life without it.

#!/usr/bin/env perl -w
use strict;

&birthday("25th");

sub birthday {
    print("Happy $_[0] birthday!\n");
}

A huge thank you to Peter Whiting who showed me Perl when I was 19, and to Larry Wall, Tom Christiansen and Jon Orwant for the language and the Camel Book :).


My first Perl CGI script website nostalgia thing!

Happiness is a stack of new interesting computer books!

If you've been on the net for a while, chances are your development history roughly follows mine! I've had people asking about my Perl script that I've lovingly referred to here over the years, so here's something :).

Ah the innocence of youth...

Before we go any further, yes that was my bedroom back in Singapore. I still have that lava lamp, that MacBook Pro and my beloved O'Reilly books :)

After graduating from GeoCities and the like to my own web server, I wrote a fairly simple Perl CGI script to run my site before finally moving over to dedicated CMSs including RapidWeaver, MediaWiki, WordPress and TextPattern. Since then, I've dabbled in Ruby/eRuby/eRubis, Ruby on Rails, Django and PHP, though admittedly my biggest strengths still lie in the back end with running and building VPSs or uni servers with FreeBSD, CentOS, Solaris, Apache, Lightty, *SQL and the like.

While not the most high performing or scalable systems, my Perl CGI script was a lot of fun! In a nutshell, it worked like this.

  1. Each of my blog posts was stored as a text file with the name of file corresponding to its stub, such as "shimapan.txt". The files themselves were simply formatted as post name, date, category and content, with newlines delineating each. No fancy markdown, just HTML sans paragraph tags!

  2. When a page was requested, the script would attempt to open its corresponding file. If this operation failed, a 404.txt file was returned in its place.

  3. The script would pull the text, wrap each paragraph in <p> tags, open a rudimentary "template" text file and insert it between two HTML comments.

  4. For example, when using the teal Hatsune Miku theme, going to http://rubenerd.com/?get=shimapan would put shimapan.txt into the mikutheme.txt file, then display a result any otaku would be proud of.

It just occurred to me this was also coded before Miku's time. Or even Akiyama Mio. I should have made a joke about Haruhi Suzumiya and bunny girls instead. You live and learn.

Processing and such fun

I eschewed (gesundheit) the CGI.pm module, mostly because I found it easy enough to create the standard "Content-type: text/html" and other headers myself. I was (and still am) a fan of minimalism, and my C training at the time made me adverse to using third party modules and APIs, especially when it was easy enough to do something myself.

The theme files and the posts themselves were uploaded to the server using SVN; they were "committed". To this day, I still think that was a really nifty way to do it. I've seen some people doing that with Git now.

Not showing any more than this, sheesh!

Learning from bad example?

I learned a lot from that site. Shortly after going live, I noticed thousands of hits to the 404.txt file. Obviously, bots had found my URL, and were filling random junk into it. I wrote in some rules that a stub could only be up to 24 characters long, and only contain alphanumeric characters. Any requests that didn't meet that criteria were automatically dropped.

I also learned about FastCGI. Unbeknownst to me in my fresh-out-of-high-school innocence, each time someone accessed my poor web server, Apache was spawning an entirely new CGI process to handle their request. Admittedly my site only started to get traction long after I ditched the script and moved onto dedicated CMSs, but had I kept using it I'm sure I would have been in for a nasty surprise!

There were also other performance issues. I never quantified how much of a hit my server was taking by having to open and close theme and data files each time, though I suppose I could have written a simple SQLite backend and compared it.

I also wasn't doing any caching; ideally once a post was completed and the theme set in stone, the script could have generated pages upon the post being uploaded, and served those instead. IIRC, this is how MovableType operated back in the mean old days. There weren't any dynamic parts to the site such as comments systems, tags and so on, so they would only need to be updated if the theme changed.

Justification!

Why am I posting about this, years from now? Because after writing a fairly basic Ruby on Rails install, playing with Django for a university assignment last semester, and my obsessive Wikipedia editing, I've decided to trial running Rubenerd.com off TextPattern! But as all infuriating entries end: that's for another post.


Puts for old versions of Perl?

Ruby developer complaining to me today that Perl doesn't have a puts method.

#!/usr/bin/env/perl -w

use strict;

my $goto = "K-K-K-K-Kathmandu!";
&puts("I think I'm going to...", $goto);

sub puts {
    print "@_\n";
}

Problem solved! ^_^


Okay okay, I'll start using Ruby again!

With this huge move and all the unexpected problems we've had, I've had very little time these last couple of months to do much programming... or blogging! I can't wait, I'm posting this from my phone.

More history you don't care about

When I left high school in 2005, my first proper job was to write Perl scripts for possibly the friendliness boss I've ever had. He absolutely loved the language, and his enthusiasm rubbed off on me! I learned to LOVE CPAN and though I never did submit any of my own ACME modules I sure created lots of them! If I were learning the language today, I'd write one that just prints The Bird is The Word until the hapless user types Papa Ooom Maw Maw Maw. ACME::Trashmen, I can just see it now!

Anyway after that I started part one of my studies and was inundated with Java. I use the word inundated because it did feel like a flood! I could understand what was going on and appreciated how the language was in some ways self documenting, but the huge, narly long lines of camel case drove me batty. Features like linked lists and generics<T> seemed nice, but felt more like a tacked on after thought in an attempt to look more like C#, a language I did in high school when I was a .net guy and didn't really care for.

Then my mum really went south and programming took a back seat to amateur nursing. Then when she left I went through what I've retroactively dubbed The Ruben Troubles. But enough about that.

Yukihiro Matsumoto is really cute

Around that time while I was taking a break from university for aforementioned family reasons, I picked up the second edition of the pickaxe book and fell in love. Ruby was like Perl but was so schweet... to the point where I was thinking "this can't be healthy!" If you'd been reading my blog for a while you would have read that I came this close to implementing my site in Rails at the time.

I don't quite know why, but as if my brain is terrified of becoming specialised (and therefore successful) despite really liking Ruby I kept tinkering around with Python as well, and eventually grew to like it more. Despite the terrible design of my site here and the fact I like to ramble on continuously without not so much as one cohesive thought or succinct statement, I'm a huge fan of minimalism and Python is such a clean language. Not only that, it uses the white space for something! I mean, we all indent our code, it makes SO MUCH SENSE to USE it! Ruby still needs end statements or braces just like C and Basic, what's up with that!?

Oh yeah now I remember why I got into Python: Django. Django is one of the nicest frameworks I've ever used. We just clicked. I really tried to like Rails and did my fair share of work with it, but we didn't click. Despite what some mathematicians and computer scientists say, I think programming is a deeply personal thing, and sometimes things just click, and sometimes they don't. I've done equal amounts of work in Ruby and Python (and WAY MORE in Perl and Java, groan!) and it just makes more sense to me.

This heading is just as useless as the other two

Ruby has some very beautiful language constructs (the iteration block is so friggen elegant and nice I want to give it a huge hug), but an equivalent Python application will [often] be smaller, a boon for people like me who do most of their development hunched over a small ThinkPad at a coffee shop or an Apple computer that has a screen that sacrifices vertical space to make it wider.

Nonetheless, for its lucrative advertising potential (RUBEN DOES RUBY!) and to shut you all up (Ruben... why don't you do Ruby? Hey Ruben, do Ruby! Hey Ruben, Python isn't for you, Ruby is! Hey Ruben, your name is a total fit! Ruben, where did I leave my keys?) I've decided to give it another shot and submit my next assignment in it instead of Python. Who knows, it'd be great to do some RubyCocoa stuff :).

#!/usr/pkg/env ruby -w

class MugiChan
  def to_s
    return "Can Yui have this guitar cheaper?"
  end
end

if __FILE__ == $PROGRAM_NAME
  waifu = Mugichan.new
  puts waifu
end

Sent from my iPhone
(one of these days I'll get rid of this thing)


Perl 6 Rakudo Star

So it looks as though Perl 6 might be coming soon... again!

#!/usr/bin/env perl -wT
use strict;

&sayWooHoo("again");

sub sayWooHoo {
    print ("Hey, 6 might be coming soon, $_[0]!");
}

My first job out of high school was writing Perl programs. Was the happiest time of my life, because university hadn't crushed my spirit yet. Perl is awesome. CPAN is awesome! And don't believe what they say, it is possible to write Perl programs that you and others can understand later, provided you follow the conventions :).


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?


#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 :).


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!