Cool Perl 6 features available in Perl 5

Today I saw Damian Conway giving a talk at YAPC::NA 2016 (also known as The Perl Conference now :-). He was talking about some cool Perl 6 features, and I realized that some of them are available right now in Perl 5.

  • Parameter lists / "signatures"

    Instead of manually unpacking @_, you can just write sub foo($bar, $baz) { ... } to define a function with two arguments.

    This feature is available in core perl5 since version 20 (the syntax changed slightly and it produces better error messages since version 22). It's still experimental in version 24 (and produces corresponding warnings when enabled).

    However, the CPAN module Function::Parameters adds full support for parameter lists to every perl since 5.14 (albeit with a new keyword (fun and method) instead of sub). It's available right now and not experimental:

    use Function::Parameters qw(:strict);
    fun foo($bar, $baz) {
  • Keyword arguments / named parameters

    By defining your subroutine as sub foo(:$state, :$head, :$halt) {}, you can call it as

        head  => 0,
        state => 'A',
        halt  => 'Z',


        halt  => 'Z',
        state => 'A',
        head  => 0,

    or any argument order you like. You no longer have to remember the position of each argument, which is great, especially if your function takes more than 3 arguments (or you haven't touched the code in a month or three).

    This is also available in Function::Parameters from perl 5.14 onwards:

    use Function::Parameters qw(:strict);
    fun foo(:$state, :$head, :$halt) {
  • Interpolating blocks in strings

    Perl 5 lets you interpolate variables in double-quoted strings, which can be very convenient:

    say "$greeting, visitor! Would you like some $beverage?";

    However, this is limited to variables (scalars and arrays/array slices). There's no way to directly interpolate, say, method or function calls. That's why Perl 6 lets you interpolate arbitrary code in strings by using { blocks }:

    say "2 + 2 = {2 + 2}";  # "2 + 2 = 4"

    This feature is available in Quote::Code on CPAN for all perls since 5.14:

    use Quote::Code;
    say qc"2 + 2 = {2 + 2}";
  • Funny Unicode variable names

    One of the examples (Pollard ρ-factorization) uses (that's a Greek lowercase rho) as a variable name because Perl 6 supports Unicode in programs by default.

    Perl 5 doesn't. By default, that is. But after a use utf8; declaration, you can put arbitrary Unicode text in your string literals, regexes, etc. And it works for variables, too:

    # this works on any perl version >= 5.8
    use utf8;
    my $ρ = 42;
    print $ρ, "\n";

Of course there were many, many other things that are not so easily ported from Perl 6. But I think it's nice how much Just Works (or can be made to work with minimal effort) in existing Perl 5 code.


I wonder if there's something that would allow the a superscript number be interpreted as an exponent (also part of his talk).

You don't need Quote::Code:

say "2 + 2 = @{[2 + 2]}"; # "2 + 2 = 4"

That has worked in perl5 for quite a while—I think the newest feature used there is "say".

Leave a comment

About mauke

user-pic I blog about Perl.