Exploring Perl 6: Signatures, Part 2

| 2 Comments | No TrackBacks

In Part 1 of our exploration of signatures in Perl 6, we explored how we could use Perl 6's powerful and flexible type system to constrain how named and positional arguments could be passed to subroutines and methods. We also covered how to use slurpy signatures to create variadic functions that can take arbitrary named and positional lists of arguments.

Perl 6's signature system offers a lot more, however. In this post we'll examine some of the more advanced features that make Perl 6's calling semantics so powerful.

Exploring Perl 6: Signatures, Part 1

| 3 Comments | No TrackBacks

Perhaps one of Perl 5's most glaring oversights is its lack of formal subroutine parameter lists. There are a number of solutions on CPAN that attempt to solve this problem with various syntactic sweeteners of diverse complexity and style. In recent releases of Perl 5, there is even an experimental signature system in the core. But all of them — ultimately — come down to unpacking the magical @_ array into lexical variables for the function to use.

Perl 6 has a sophisticated, robust, and flexible signature system for subroutines and methods. Let's check it out.

Exploring Perl 6: Meta Operators

| No Comments | No TrackBacks

In our last Exploring Perl 6 post, we took a brief look at hyper operators, which allow you to apply binary or unary operators through lists as a single operation with support for automatic concurrency.

The circumfix << >> operator which transforms a normal operator into hyper-mode is actually just one of a class of operators that change the behavior of other operators. In other words, meta operators.

Let's take a look at a few more meta operators and how they work.

Exploring Perl 6: Hyper Operators

| 1 Comment | No TrackBacks

Like Perl 5, Perl 6 has a wealth of builtin operators for transforming values. In addition to the standard complement of arithmetic, logic, and bitwise operators that any programming language should have, Perl 6 also has a number of operator categories not commonly found in procedural or object-oriented languages, which can make solving certain problems extremely simple. Among these are hyper operators, a set of list-optimized transforms that automatically benefit from Perl 6's autothreading.

Exploring Perl 6: Numeric Types

| No Comments | No TrackBacks

Perl has always been pretty fuzzy with types, allowing you to convert things between numeric and string values transparently and automatically. Usually, Perl does the right thing, except when it doesn't. If you've ever messed about with Perl 5 internals, you know that there are actually several numeric types hiding behind the Perl programmer's concept of a scalar. Perl 6 works largely the same way with regard to automatic type conversion, but adds some optional type constraint checking and more robust numeric types.

Exploring Perl 6: Sigil Invariance

| 2 Comments | No TrackBacks

Perl programmers are used to sigils (the funny characters in front of variables) being an indicator of the kind of thing returned by variable expressions. $ means scalar, a single thing, @ means array, an ordered list of things, and % means hash, an associative array. When retrieving a single value from an array or hash the sigil changed to $. When retrieving a slice from a hash, the sigil changed to @.

In Perl 4, all of this made a strange kind of sense. It worked like indicators of quantity in natural languages: the following thing is either singular or plural. In Perl 5, with the addition of scalar references and objects, sigils became inconsistent and confusing. Perl 6 solves this problem by doing away with sigil variance altogether to provide a more consistent and understandable variable syntax.

Exploring Perl 6: Up and Running

| 4 Comments | No TrackBacks

Happy new year!

After a remarkably long wait, several false starts, and an unfathomable amount of work by the community, Perl 6 is here. Let's see just how easy it is to get up and running and to write a few simple programs.

Jargon

Before we get started, let's clarify some terminology.

  • Perl 6 is a programming language specification. It's not the next version of Perl 5 (which remains under active development) but a whole new language.
  • Rakudo is a compiler implementing the Perl 6 specification. There are other Perl 6 compilers, but Rakudo is the only one that is complete today.
  • MoarVM is one of the virtual machine architectures targeted by Rakudo. Rakudo also targets the JVM, but the MoarVM backend is considered more stable at this time.
  • Rakudo Star is a distribution of the Rakudo compiler plus a bunch of Perl 6 modules and documentation to make it useful. These blog posts will focus on the use of Rakudo Star.

About once a quarter, MongoDB conducts a two-day Skunkworks session. Half-hackathon, half-incubator, the idea is to take a break from our day-to-day tasks and work feverishly for 48 hours on any project we want. Some projects are undertaken to scratch a particular itch and make our core products better. Others are for use inside the company to make our lives a little easier or more amusing. Still others are just for the hell of it. This quarter, my teammate Andrew Emil and I decided to work on something in the last category.

I recently started using Jeffrey Thalhammer's excellent Stratopan to manage CPAN dependencies for a new Perl project. I had played a little with Stratopan before, but now that I'm starting to get some use out of it, I am even more impressed with Jeffrey's work.

Stratopan allows you to create your own CPAN-like site in the cloud, which you can use to maintain a stable basket of dependencies for your project. You won't have to worry about new versions of distributions sneaking onto your private CPAN and breaking your stuff. Even better, you can upload your own, private CPAN-like distributions, and now you have the entire CPAN toolchain available to manage your internal codebase.

The one shortcoming I ran into was releasing my own distributions directly to Stratopan. Stratopan has a web interface where you can upload distribution tarballs, but I wanted to just be able to type dzil release, just like I do to make public CPAN releases. Those make use of Dist::Zilla::Plugin::UploadToCPAN, but there was no equivalent for Stratopan. Until now!

Removing Perl Boilerplate with Import::Into

| No Comments | 1 TrackBack

I recently started a new Perl project and wanted to take advantage of some cool new Perl 5.20 features. In particular, I wanted to use the experimental subroutine signatures feature and postfix dereferencing. That requires the following incantation:

use feature 'signatures';
use feature 'postderef';
no warnings 'experimental::signatures';
no warnings 'experimental::postderef';

That can be abbreviated a bit by passing a list to feature and just turning off the entire experimental class of warnings:

use feature 'signatures', 'postderef';
no warnings 'experimental';

But it's still a couple lines of boilerplate that need to be pasted atop every file in my project. I hate pasting things.

Recent Comments

  • Yary: I found the relevant P6 Signature docs about constraining to read more
  • Yary: For the type specifiers, isn't there also a way to read more
  • Mike Friedman: Hi Jim, One simple example is to place a type read more
  • James E Keenan: "The $x is not just a loop variable, it's actually read more
  • James E Keenan: A little example of mandated named parameters: ########## #!/usr/bin/env perl6 read more
  • James E Keenan: Mike, you wrote: ##### > (opposing form). Either list can read more
  • James E Keenan: The installation examples worked fine on my Ubuntu Linux read more
  • James E Keenan: Mike, this looks like a very promising and useful guide read more
  • Mike Friedman: Thanks, Neil! read more
  • NeilH: Mike, Glad to see your back blogging...... Excellent article. read more

Recent Assets

  • camelia-logo.png
  • Skunkworks trophy

Categories

  • Exploring Perl 6

Pages

Powered by Movable Type 5.14-en