Make bitwise operators always use numeric context

[This feature is no longer experimental, starting in v5.28. Declaring use 5.28 automatically enables them.]

Most Perl operators force their context on the values. For example, the numeric addition operator, +, forces its values to be numbers. To “add” strings, you use a separate operator, the string concatenation operator, . (which looks odd at the end of a sentence).

The bitwise operators, however, look at the value to determine their context. With a lefthand value that has a numeric component, the bitwise operators do numeric things. With a lefthand value that’s a string, the bit operators become string operators. That’s certainly one of Perl’s warts, which I’ll fix at the end of this article with a new feature from v5.22. Continue reading “Make bitwise operators always use numeric context”

Perl v5.22 adds hexadecimal floating point literals

You can specify literal hexadecimal floating-point numbers in v5.22, just as you can in C99, Java, Ruby, and other languages do. Perl, which uses doubles to store floating-point numbers, can represent a limited set of values. Up to now, you’ve had to specify those floating point numbers in decimal, hoping that a double could exactly represent that number. That hope, sometimes unfounded, is the basis for the common newbie question about floating point errors. Continue reading “Perl v5.22 adds hexadecimal floating point literals”

Use Perl 5.22’s <<>> operator for safe command-line handling

We’ve had the three argument open since Perl 5.6. This allows you to separate the way you want to interact with the file from the filename.

Old Perl requires you to include the mode and filename together, giving Perl the opportunity to interpret what you mean: Continue reading “Use Perl 5.22’s <<>> operator for safe command-line handling”

Perl 5.22 new features

The first Perl 5.22 release candidate is out and there are some new operators and many enhancements to regular expressions that look interesting, along with some improvements that don’t require any work from you. Some of the features are experimental, so be careful that you don’t create problems by overusing them until they settle down. Continue reading “Perl 5.22 new features”

Use v5.20 subroutine signatures

This is a chapter in Perl New Features, a book from Perl School that you can buy on LeanPub or Amazon. Your support helps me to produce more content.



This feature was promoted to a stable version in v5.36.

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.

Perl v5.18 adds character class set operations

Perl v5.18 added experimental character code set operations, a requirement for full Unicode support according to Unicode Technical Standard #18, which specifies what a compliant language must support and divides those into three levels.

The perlunicode documentation lists each requirement and its status in Perl. Besides some regular expression anchors handling all forms of line boundaries (which might break older programs), set subtraction and intersection in character classes was the last feature Perl needed to be Level 1 compliant. Continue reading “Perl v5.18 adds character class set operations”

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”

Enforce ASCII semantics when you only want ASCII

When Perl made regexes more Unicode aware, starting in v5.6, some of the character class definitions and match modifiers changed. What you expected to match \d, \s, or \w are more expanvise now (Know your character classes under different semantics). Most of us probably didn’t notice because the range of our inputs is limited. Continue reading “Enforce ASCII semantics when you only want ASCII”

Perl v5.16 now sets proper magic on lexical $_

[Lexical $_ was removed in v5.24]

Perl v5.10 introduced given and the lexical $_. That use of $_, which everyone has assumed is a global variable, turned out to be a huge mistake. The various bookkeeping on the global version didn’t happen with the lexical version, so strange things happened. Continue reading “Perl v5.16 now sets proper magic on lexical $_”