Perl in CURRENT_YEAR
Perl is a programming language that will always live deep into my heart. It was the first programming language I can say I mastered. But it’s sad that not even God uses this programming language anymore. It’s a great programming language, it has evolved in the latest years, so much that it even looks like a modern programming language. Some of the stupid design of perl has been fixed.
It is known by everyone that the perl Syntax sucks. It makes 0 sense, at least for learners. Experimented perl programmers no longer fear anything.
For comments or questions on this post, email me at sukamu at riseup dot net.
Function prototypes
Perl always lacked function signatures (prototypes), the way you passed arguments to a function in perl was horrible, it is like functions in bash:
1: sub sum() { 2: my ($x, $y) = @_; 3: return $x + $y; 4: } 5: 6: print sum(3, 4); # Prints 7
You define the function parameters inside the function, just like in
bash. This is, in my opinion, horrible, but since perl 5.36, you can
have function signatures, to use perl’s new features, you have to
specify the version of perl you want to use, to do this just use the
use
keyword.
1: use v5.36; 2: 3: sub sum($x, $y) { 4: return $x + $y; 5: } 6: say sum(4, 5); # prints 9, and adds a newline.
The function signature feature was added in Perl v5.20, but it started
to be considered stable at v5.36. There is also this new “say”
function that is just like print
but adds a newline
at the end of
the line, like python’s print
or C’s puts()
. These features were
inhereted from Raku.
Try/Catch
Perl added an experimental Try/Catch blocks in v5.34. These are still
considered experimental as of v5.36. But you can still use them. The
try/catch
(also finally
if you’re using v5.36) works like any
other programming language that has try/catch blocks. As this feature
is still experimental and should not be used seriously, you have to
specify that you want to use the experimental feature 'try'
:
1: use v5.36; 2: use strict; 3: use warnings; 4: 5: use experimental 'try'; 6: 7: sub attempt() { 8: say "I'm doing something!"; 9: die "Goodbye cruel world"; 10: return 42; 11: } 12: 13: try { 14: my $x = attempt(); 15: } catch($e) { 16: say "Exception: $e"; 17: } finally { 18: say "Finished"; 19: }
And the thing does exactly that, the script dies inside the try
block, so it’s passed to the catch
block as an exception. And
finally, the finally
block is executed.
the isa
operator
The isa operator tests whether an object has a instance of a certain
class, in non-object programming slang, it just checks the type of a
variable. The isa
operator was introdouced as an experimental
feature in perl v5.34.0 and was no longer considered experimental in
the current v5.36.0.
1: #!/usr/bin/perl 2: 3: use v5.36; 4: use LWP::UserAgent; 5: my $ua = LWP::UserAgent->new; 6: 7: 8: if ($ua isa LWP::UserAgent ) { 9: say "LWP::UserAgent object instanced successfully"; 10: } else { 11: die "something that should not have happened happened."; 12: }
Before this operator, you had to use the ref
function and string
checker to accomplish the same thing, like this;
1: use LWP::UserAgent; 2: my $ua = LWP::UserAgent->new; 3: 4: if(ref($ua) eq "LWP::UserAgent") { 5: # Do stuff... 6: } else { 7: # Do stuff when failed. 8: }
Which clearly makes a difference at the moment of reading and writing new code.
The defer block
This is a feature that others programming languages have, C++ calls it RAII, there are plans to add it to the next version of the C programming language, also golang has the defer block. Thing is that in perl it works like this:
1: #!/usr/bin/perl 2: 3: use v5.36; 4: 5: use experimental 'defer'; 6: my $x = 0; 7: { 8: $x = (2^31)-1; 9: defer { 10: say "The block has finished it's execution"; 11: $x = 0; 12: }; 13: say "Let's pretend i'm doing stuff..."; 14: say "x value: $x"; 15: } 16: say "x value: $x";
Simillary to try/catch
, this function is experimental and should not
be used in production. Or use it if you want. But don’t blame the perl
devs if something breaks (which, honestly, I doubt that your system
will break because of an experimental feature. Just don’t abuse it or
use it in very weird ways and you’ll be fine).
builtin values
builtin
is a new core module that shipts with perl v5.36. This
package are plains function and behave like user defined
cuntions. They do not provide any special thing you have never seen in
perl before. But they’re still good to have, as they help to make the
code more easy to write and read. These functions are also
experimental so be careful at the moment of using them.
1: use v5.36; 2: use experimental 'builtin'; 3: use builtin qw(true false is_bool trim 4: reftype created_as_string 5: created_as_number); # There are more builtin functions... 6: my $bool_value = true; 7: 8: if ($bool_value) { 9: say "The value is true"; 10: } else { 11: say "The value is false"; 12: } 13: 14: say is_bool($bool_value); # Will print "true"; 15: 16: my $str = "\t\n\r Hello world\t\r\n"; 17: $str = trim($str); # This trim() function will remove all the whitespace 18: #characteres defined by unicode (https://perldoc.perl.org/perlrecharclass#Whitespace) 19: 20: print $str; # Will print "Hello world", without newlines, tabs, or 21: # carriage returns.
These new functions are not the greatest or most useful thing ever, but they are still helpful.
Conclusion
Perl’s not dead. At least that’s what I kept telling myself because I don’t want to see this programming language dying in my lifespan. But it’s pretty cool that they still add new features to Perl 5. Despite everything you could say, perl is still alive, but no one uses it, but well, no one uses Haskell either. :^)