Pushing the envelope

Darren's take on Java, agile methods, cool open source stuff, interesting technologies and other random wanderings through the land of blog.
Updated: 26/01/2003; 11:51:03.
Places to go
Apache Jakarta Project
c2.com
ExtremeProgramming.org
OpenSymphony
XProgramming.com
XP Developer

People to see
Russell Beattie
Eugene Belyaev
Tony Bowden
Mike Cannon-Brookes
Jeff Duska
Paul Hammant
Scott Johnson
Brett Morgan
Rickard Öberg
James Strachan
Joe Walnes

Things to do

Subscribe to "Pushing the envelope" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.


That was the day
November 2002
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Oct   Dec



Archives
December 2002
November 2002
October 2002
September 2002
August 2002

Listening To


Valid RSS

Click here to visit the Radio UserLand website.

  28 November 2002

Going retro

Blocks in Java and C#

James talks about some syntactical sugar enhancements that can be made to Java borrowed from C#. Although it sounds petty, this stuff really makes a difference when you're staring at code all day. Clean and readable code makes for relaxation which makes for smarter thinking. Syntax is closely related to karma.

The foreach and using operators are particularly nice as it provides a cleaner sequence for commonly executed sequences of events. But why stop there?

One of the features that made Smalltalk and Ruby so popular was the use of blocks. These allow you to create your own constructs.

Having (finally) twisted my brain far enough to start to understand Perl, I'm actually starting to appreciate it. This alone should probably scare me. It is (proudly, deliberately) a derivative language.

Perl shamelessly borrows paradigms from C, C++, Lisp and a few Unix tools. I'm still learning, but it looks like a lot of the AOP, block/closure & attribute stuff could easily be (or has already been) implemented in Perl, and has been available for years. I sometimes wonder if language designers are doomed to reinvent things that some guys over the road already implemented a decade ago.

Take this:

my $block =
sub {
my ($target) = (@_);
print "Hello, $target!\n";
}

Dangerous Perl Oversimplification 101:
my is like 'private'.
$ means 'scalar' - essentially a single variable.
my ($target) = (@_) means (roughly) 'take a copy of the first argument and assign it to $target in list context'.

$block now holds a reference to a function that will print a greeting every time it gets called. You can treat it like any other kind of variable, including sticking it in an array, or indeed a hash. Dynamic dispatch becomes a simple matter of using your arguments as the key to a hashmap. Yes it can be done in strongly typed languages with some creative use of interfaces and the Command pattern. The point is, its built-in, and has been for some time.

Why is Perl hard?

  • Well for a start, it has no grammar. You can't generate a parser for Perl, its too irregular.
  • It is contextual. Like English, meaning can vary depending on the topic of conversation. For example, if I ask you "where are you going?", and you replied, "to the shops", we would both know that it was you we were talking about, and that you were going to the shops. Perl is a bit like that. A lot of those evil looking magic variables are to do with this. $_ is essentially the 'topic' under discussion. Unless told otherwise, most Perl operators assume $_ which is one of the biggest hurdles to overcome when learning Perl.
  • Perl is laid-back. So lines like 'unless ($flag) {return 0};' can also be written 'return 0 unless $flag;'.

XP in Perl?

Can be done, of course. In a language as powerful and undisciplined as Perl, the discipline of XP is more important than ever. Refactoring will be harder than in other languages, possibly so much so that the (heretofore undiscovered) 'refactor to other language' may need to be applied. Test first is more rapid than in languages with a compilation step as you might expect.

One of the most compelling reasons to use Perl? CPAN. As has been said elsewhere, the true strength of many modern languages lies in the extent of their libraries. Chances are, if you need to do something in Perl, someone else has already done it and put it in CPAN. Something that made me laugh today is the Date::Manip module, which can parse strings like "second wednesday in 1996" to date values.

Take a walk on the wild side.


12:16:25 AM      comment []

© Copyright 2003 Darren Hobbs