Perl – The Ultimate Programming Language :-)

1 Comment

Ok, I really really tried hard to avoid any exaggerations in the title of this blog post and I strongly believe ultimate is a more than proper description of what is for sure the best programming language on the planet. Maybe the universe … but let’s wait and see :-).

How was the idea for this “Fun with Perl” blog post born? Friday two weeks ago two colleagues of mine where showing me – very enthusiastic – some cool tricks in Clojure. Most of them dealing with number ranges that could be easily created and manipulated. Hey, these could be some real-life problems somewhere as I strongly believe there are infinite parallel universes and in one of them dealing with number ranges will be helpful; for sure :-).

After showing me how easy it is to select all odd numbers from a range of numbers there was of course only one answer to this: In Perl I can do that as easily in three minutes, Perl is unbeatable :-)! Of course my young colleagues did not believe me and the result was the following little Perl script that does the trick. Ok, to be honest, I guess it took me five minutes, because I needed to download and install the Set::IntSpan package using the Perl Package Manager.

use Set::IntSpan;
$set = new Set::IntSpan '0-10';
@range = elements $set;
@odd = grep { $_ & 1 } @range ;
print join(", ", @odd);
# result: 1, 3, 5, 7, 9

Did I already mention that Perl has a cool Package Manager to install new libraries? Good :-)! Then the grep-function in Perl is incredibly powerful and what could be more obvious than doing bit-operations here?

Ok, who is … √§hm … I mean what’s next? Oh I know, applying functions to a list of elements. I think I have seen something like this or heard that this now the cool sh$t in all the fancy new programming languages out there. And here comes Captain Perl to the rescue:

@names = qw(max thomas rolf gerrit);
@prettyNames = map { ucfirst($_) } @names;
print join(", ", @prettyNames);
# result: Max, Thomas, Rolf, Gerrit

Nice, but only something to warm up for Perl. What about having a comma-seperated list of colon-seperated entries in a String. Make a hashmap out of this in zero programming time including some basic syntax checking using – of course – the grep-function:

$mapping = "Rolf:1,Gerrit:2,Max:7,SyntaxWrong9,Thomas:14";
%hash = map { split(/:/, $_) } grep {m/:/} split(/,/, $mapping);
while ( ($key, $value) = each %hash ) {
  print "key: $key, value: $hash{$key}\n";
# result: 
#key: Thomas, value: 14
#key: Rolf, value: 1
#key: Max, value: 7
#key: Gerrit, value: 2

Enough of this because I mean, you might have considered another programming language cool and of course I do not want to frustrate anyone here :-). Let’s take a look at operators. Do you know any other language having a spacestation– and a spaceship-operator? Let’s take a look at the spacestation-operator that turns Strings starting with a positive number into a number:

print -+-"5e2";        # result: 500
print -+-"14Thousand"; # result: 14
print -+-"3.14zzz";    # result: 3.14

Ok, that is more on the fun-side of it, but let’s look at an operator that is really really cool, the defaults-operator:

$val ||= 5;
print $val;
# result 5

Looks not very spectacular, right? But handling default-values using if/then/else-constructs is not really the nicest thing to do. So let’s take a look at a bit more elaborate example:

sub calculateValue {
	$a = @_[0];
	$b = @_[1];
	print "new calculated\n";
	return $a + $b;
$hashed{x} ||= calculateValue(1, 2);
$hashed{x} ||= calculateValue(1, 2);
$hashed{y} ||= calculateValue(2, 3);
# result:
# new calculated <- First call for x
# new calculated <- Call for y

The above example gives an impression on the power of this operator that can make a lot of boilerplate code superfluous.

Let’s close this blog post with what is probably the most poerful feature in Perl: eval! Using eval it is possibile to execute Perl-code from within a program. Let’s take a look at a very simple example first with which one would probably already be the king in every “Extreme Startup Coding Dojo” (I think I will attend the next one using Perl:-).)

$calc = "1+5+7";
print eval($calc);
# result: 13

The following example shows a bit more of what is possible with this. In principle you could even load complete code-parts from a file or database – of course using strict security mechanisms – and then execute them depending on certain conditions. The possibilities this offers are more or less infinite :-).

$code{1} = 'print "Method 1\n"';
$code{2} = 'print "Method 2\n"';
# result: 
# Method 1
# Method 2

Ok, that should be enough fun for today. Of course things are done here quick and dirty, but there are means in Perl to limit the quick and dirty approach while still being able to use the power of its language possibilities. For sure one will always use things like “use strict” and “use English” to have more readable names for Perl’s internal variables and to be forced to define variables, so that a typo in a variable name does not lead to hard to track errors. In the end it is even possible to implement object-oriented with Perl, but this is then something for another Perl-related blog post :-).



  • 9. September 2015 von Bernhard Schmalhofer

    There is a discussion about this article on Reddit, https://www.reddit.com/r/programming/comments/3k2fdv/perl_the_ultimate_programming_language/.

    Here are my $ 0.02 worth.

    I strongly recommend to enable “use strict” and “use warnings”, even for these short snippets. These pragmas realy help to avoid some of the pitfalls that do exist in Perl.

    In Perl arguments are passed to subs as aliases in the array @_. It is good practice to unpack @_, as the arguments shouldn’t inadvertedly be changed inside the sub. However using the one element array slice @_[0] is not sane. Better is using $_[0]. Even better would be my ($a, $b) = @_;


Your email address will not be published. Required fields are marked *