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 $_”

Look up Unicode properties with an inversion map

Perl comes with extracts of the Unicode character data, but it hasn’t been easy to look up all of the information Perl knows about a character. Perl v5.15.7 adds a way to created an inverted map based on the property that you want to access.

Continue reading “Look up Unicode properties with an inversion map”

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”

Loose match Unicode character names

The charnames module can now handle loose name matching, as outlined in Unicode Standard Annex #44. This accounts for the various ways people are abusing things.

Consider the character 😻, (U+1F63B SMILING CAT FACE WITH HEART-SHAPED EYES). If you want to interpolate that into a string, you have to use the exact name:

use v5.16;
use open qw(:std :utf8);

say "\N{SMILING CAT FACE WITH HEART-SHAPED EYES}";

Starting with v5.16, the \N{} in a double-quoted string automatically imports :long and :short. There’s another one that you can import yourself, but it’s a bit costly.

Some people don’t like all uppercase strings, so they might want to type it out as title or lowercase:

use v5.16;
use open qw(:std :utf8);

say "\N{Smiling Cat Face With Heart-Shaped Eyes}";

That doesn’t work and you get an error:

Unknown charname 'Smiling Cat Face With Heart-Shaped Eyes'

Import :loose from charnames and it will works:

use v5.16;
use open qw(:std :utf8);
use charnames qw(:loose);

say "\N{Smiling Cat Face With Heart-Shaped Eyes}";

The loose naming rules involve three things, which makes the loose matching slow:

  • Ignore case folding
  • Ignore whitespace
  • Ignore “medial” hyphens (letters on either side)

So all of these work, even the one with consecutive hyphens:

use v5.16;
use open qw(:std :utf8);
use charnames qw(:loose);

say "\N{Smiling Cat Face With Heart Shaped Eyes}";
say "\N{SmilingCatFaceWithHeartShapedEyes}";
say "\N{Smiling-Cat-Face-With-Heart-Shaped-Eyes}";
say "\N{Smiling----Cat-Face-----With-Heart-----Shaped-Eyes}";

Some problematic names

This doesn’t work out well for some names, and Perl developer Karl Williamson made some comments about this to the Unicode Consortium in 2010. There are some names that have hyphens next to whitespace (so, not medial hyphens), but if you ignore whitespace first, then the hyphen isn’t next to whitespace.

Not only that, removing the hyphen can turn it into a character’s name into that for a completely different character:

  • U+0F68 TIBETAN LETTER A
  • U+0F60 TIBETAN LETTER -A
  • U+0FB8 TIBETAN SUBJOINED LETTER A
  • U+0FB0 TIBETAN SUBJOINED LETTER -A
  • U+116C HANGUL JUNGSEONG OE
  • U+1180 HANGUL JUNGSEONG O-E

Hide low-level details behind an interface

Perl 5.16 makes the Perl special variable, $$, writeable, but with some magic. That’s the variable that holds the process ID. Why would you ever want to do that? There’s not much to write about with this new feature, but there’s plenty to write against it since it introduces more magic (see commit 9cdac2 on June 13, 2011). Continue reading “Hide low-level details behind an interface”

Perl 5.16 new features

The Perl 5 Porters are currently working on Perl 5.15, the development track that will end up as Perl 5.16. By reading the perldelta515* documentation, you can get a peek at what will mostly likely be in the next maintenance version of Perl. You need to read each of the perldelta5* in a development series because they only document the changes in to the previous development release, not the cumulative changes since the last major release.

We’ll cover these features, although we might have to stretch some of the posts a bit since some of these aren’t that radical: