A Perl of Great Price

Posted by Rick DeNatale Wed, 16 Aug 2006 15:03:18 GMT

An old programming adage goes: “You can write Fortran in any language.”

Computer languages, like natural languages, tend to spawn their own cultures and religions. And it’s not unexpected that the groups associated with these have different slants on things.

One of Ruby’s strengths is that it stole from the best. When I look at the language, it feels like Matz took the best features from a variety of languages including Smalltalk, Lisp, and not the least, Perl.

Two great features which Ruby inherited from Perl are integral support for regular expressions, and Perl’s excellent statement modifiers which allow things like:
if test
   statement
end
to be replaced with the more succinct and usually clearer:
statement if test
However, IMHO, overuse of the Perl features in Ruby can make code too Perl-like.

I’ve seen my share of Ruby code which is hard-to read and understand, and a lot of that looks suspiciously like Perl code re-written in Ruby.

A ruby, being a gemstone which is normally cut before being put to use, has facets, unlike a pearl.

The facets of Ruby, the language, let indiduals approach it from different directions. To me, as an old Smalltaker, it tends to look like Smalltalk. I tend to view it with Smalltalk-colored glasses.

I’m sure that, to a programmer deeply steeped in Perl, Ruby looks like Perl with a cleaned-up view of object-oriented features.

Literate Programming

I’ve long been a fan of the goals of literate programming. Source code should be viewed as literature, something which communcates to human readers, and not just fodder for a tool chain which produces something which is executable by a machine.

I read and write Perl on a level similar to how I converse in French. I’m told that I can speak French fairly well, and I can make myself understood. On the other hand I have a hard time understanding spoken French.

Too often, Perl looks to me like a write-only language, an impression which is not uniquely mine.

Some Perl programmers share a common predilection for something which users of another language often called read-only, APL, prided theselves on writing, one-liners. And this has inspired catalogs of Ruby one-liners

I’ve found that a lot of the literature of Perl isn’t very understandable. I’ve found that even Perl code which I’ve written myself is impenetrable when I come back to it weeks, or even days after writing it.

I’m sure that there are Perl programmers who don’t find this to be true, just like my French friends seem to be able to converse without problems, but I continue to only muddle along in both languages.

The key indicator of Perl programs written in Ruby is the use of all those global variables with funny names like $*, $&, $0, etc. Perl programmers love them since they are in Ruby for compatibility with Perl. When I see them, my eyes glaze over, and my mind says ”$&^+_”

I’m not trying to antagonize the Perl programmers among the Ruby community, anymore than I try to antagonize my francophone friends, in fact I’ve got great love for some particular francophones and perlographs.

But I’ll probably never understand them as well as I’d like to.

P.S. Not (Directly) Borrowed From Perl

One feature of Perl is the use of sigils as markers on variable names.

Ruby uses sigils to mark some variables as being globals (there’s that $ again, but the names don’t have to be funny), instance variables, or class variables.

The difference is that Perl uses sigils to distinguish the types of variables, something which is unnecessary in Ruby since everything is an object. Ruby instead uses them to find the variable binding in the case of a reference, or where to put it in the case of an initial definition. A Ruby variable comes into existance the first time it is mentioned in the execution of a Ruby program, and it goes into the global variable “pool”, the current instance, or the current instances class depending on the sigil. No declaration is necessary as compared to Smalltalk, for instance, where instance and class variables are ‘declared’ by telling the class about them, and globals are defined by putting them in the system dictionary. While Perl variables aren’t explicitly declared either, the real motivation for sigils in Perl is to associate a type with a variable.

Perl also makes use of the fact that all variables start with a sigil to automatically interpolate variables into strings. Since ruby variables don’t all start with a sigil, Ruby uses #{} as a marker to interpolate an expression int to a string. Note that if that expression is just a global, class or instance variable (all of which start with a sigil) a simple # will do the job.

So Perl uses sigils more as a way to express type, a static concept, in a dynamically executed language, and Ruby uses them to avoid static declarations, and make variable definition and use dynamic.

Posted in  | Tags , , ,  | no comments | no trackbacks

Comments

Trackbacks

Use the following link to trackback from your own site:
http://talklikeaduck.denhaven2.com/articles/trackback/19

Comments are disabled