Initialize array and hash variables with state

Perl v5.28 allows you to initialize array and hash variables that you declare with state. This is a feature a long time coming and that I’m quite happy as finally arrived.

Since v5.10 and up to v5.26 you could only initialize a state variable if it was a scalar. You could declare a hash or array variable but you couldn’t give it an initial value at the same time. You could do this:

Continue reading “Initialize array and hash variables with state”

Use subroutine signatures with anonymous subroutines

While working on my excellent number project, I created a subroutine that took a callback as an argument. When I dereferenced the callback I wanted to supply arguments. Since I was using Perl v5.22, I tried using a subroutine signature with it. Continue reading “Use subroutine signatures with anonymous subroutines”

Use v5.20 subroutine signatures

Subroutine signatures, in a rudimentary form, have shown up in Perl v5.20 as an experimental feature. After a few of years of debate and a couple of competing implementations, we have something we can use. And, because it was such a contentious subject, it got the attention a new feature deserves. They don’t have all the features we want (notably type and value constraints), but Perl is in a good position to add those later. Continue reading “Use v5.20 subroutine signatures”

Use the :prototype attribute

Perl 5.20 introduced experimental subroutine signatures. Now two features vie for the parentheses that come after the name in a subroutine definition. To get around that, v5.20 introduced the :prototype attribute.

There’s not much to this. Here’s a prototype for a subroutine that takes two arguments:

sub add ($$) { ... }

Change that to the attribute form:

use v5.20;
sub add :prototype($$) { ... }

But, you probably don’t need prototypes in the first place. You might consider simply getting rid of them altogether.

Subroutine signatures are experimental and have to go through at least two maintenance releases before they can graduate out of an experimental feature, so be ready for that. I expect that most people will have problems with module code they don’t control, so you might be at the mercy of the module maintainers.

Don’t use named lexical subroutines

Lexical subroutines are a stable feature starting with v5.26

Perl v5.18 allows you to define named subroutines that exist only in the current lexical scope. These act (almost) just like the regular named subroutines that you already know about from Learning Perl, but also like the lexical variables that have limited effect. The problem is that the feature is almost irredeemably broken, which you’ll see at the end of this Item. Continue reading “Don’t use named lexical subroutines”

Use __SUB__ to get a reference to the current subroutine

What if you want to write a recursive subroutine but you don’t know the name of the current subroutine? Since Perl is a dynamic language and code references are first class objects, you might not know the name of the code reference, if it even has a name. Perl 5.16 introduces __SUB__ as a special sequence to return a reference to the current subroutine. You could almost do the same thing without the new feature, but each of those have drawbacks you might want to avoid. Continue reading “Use __SUB__ to get a reference to the current subroutine”