Main Content

C++ and Perl - why did they do it THAT way?

Archive - Originally posted on "The Horse's Mouth" - 2006-11-16 23:14:50 - Graham Ellis

"Why did [they] do it THAT way?". It's a question often asked by the brighter and more perspective delegates on courses concerning some features of a language that I'm teaching them. And the answer "because they did" is a poor one. It's like saying to a child "because I said so" rather than looking behind the scenes for a real reason.

C++ builds up on top of C, and maintains the C syntax. So there are many things in C++ which, had it been written from a clean sheet, might not have been included. And C++ is a low level language with bit / byte / register control capability, so some of the constructs, capabilities and syntax is somewhat more cumbersome than would be ideal for many modern applications. Which is precisely why it's still much used in computation intensive type works and device drivers, etc, but has been displaced by Java and C# for many of the more mainstream uses - Java and C# both take the philosophy and approach of the C++ syntax, modifying and extending it to remove the low level and C compatibility, and other complexities who's implementation costs outweigh their usefulness in the mainstream. It's little wonder that Java and C# look very similar to each other, then, with the same starting point and same philosophy and the same market.

Perl's another language where "Why is it done THAT way" is an excellent question that is, from time to time, answered by a rather glib "for historic reasons". The use of @ as the special character to signify a list would probably NOT have been the decision made after email addresses became common, but it was an excellent choice at the time. But many (most) other answers to "why was that provided" in Perl cam come down ro a far, far better answer.

Why use \s \d and \w to represent space, digit and word characters in Perl regular expressions? Because they're very quick to type and you can get a whole lot of code in a little space ... because the \ is already designated as the 'escape' character for strings and regular expressions, and because s d and w stand for space, digit and word. But why go on to use the capital equivalent \S \D and \W for a non-space, non-digit, non-word? Now that one stumped me the first time I was asked, but I can now ask you back "what better suggestion do you have?". In practise, using the same letter in capital and lower case makes them very easy to remember once you're used to them. And the resultant regular expressions are short and elegant.

Of course, Perl is eclectic ... so you can, if you prefer, write [[:space:]] for a space character and use all of the other posix standard regular expression handlers too.

This week started with me teaching OO programming in C++ and is ending with me teaching OO programming in Perl (on our Perl for Larger Projects course). And the two philosophies and approaches make an interesting comparison. Both are right for the right application ... both are great languages and what a great deal of fun I've been having.

A series of C++ examples that put together a whole host of the facilities offered by C++ - inheritance, static, inline, public, private, const, friend, vector, namespace, new, overriding, overriding operators ... was written on the fly, and I've posted them up under our putting it all together modules. I don't have the same Perl examples to show you, as that course has still got a day to run but of course you'll find plenty from previous courses in places such as "more object in Perl" resources.