Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

Perl – The Ultimate Programming Language :-)

3.9.2015 | 4 minutes of reading time

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 .

1use Set::IntSpan;
2 
3$set = new Set::IntSpan '0-10';
4@range = elements $set;
5@odd = grep { $_ & 1 } @range ;
6 
7print join(", ", @odd);
8# 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:

1@names = qw(max thomas rolf gerrit);
2@prettyNames = map { ucfirst($_) } @names;
3print join(", ", @prettyNames);
4# 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:

1$mapping = "Rolf:1,Gerrit:2,Max:7,SyntaxWrong9,Thomas:14";
2%hash = map { split(/:/, $_) } grep {m/:/} split(/,/, $mapping);
3while ( ($key, $value) = each %hash ) {
4  print "key: $key, value: $hash{$key}\n";
5}
6# result: 
7#key: Thomas, value: 14
8#key: Rolf, value: 1
9#key: Max, value: 7
10#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:

1print -+-"5e2";        # result: 500
2print -+-"14Thousand"; # result: 14
3print -+-"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:

1$val ||= 5;
2print $val;
3# 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:

1sub calculateValue {
2    $a = @_[0];
3    $b = @_[1];
4    print "new calculated\n";
5    return $a + $b;
6}
7 
8$hashed{x} ||= calculateValue(1, 2);
9$hashed{x} ||= calculateValue(1, 2);
10 
11$hashed{y} ||= calculateValue(2, 3);
12 
13# result:
14# new calculated <- First call for x
15# 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:-).)

1$calc = "1+5+7";
2print eval($calc);
3# 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 :-).

1$code{1} = 'print "Method 1\n"';
2$code{2} = 'print "Method 2\n"';
3 
4eval($code{1});		  
5eval($code{2});
6 
7# result: 
8# Method 1
9# 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 :-).

share post

Likes

0

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.