Cool Things in Perl 6

Yeah, Perl 6 is going to allow us to do very interesting things. Given this code:

use v6;

subset Filename of Str where { $_ ~~ :f };

sub foo (Filename $name) {
    say "Houston, we have a filename: $name";

my Filename $foo = $*EXECUTABLE_NAME;

We get this output:

Houston, we have a filename: /Users/ovid/bin/perl6
Houston, we have a filename: /Users/ovid/bin/perl6
Constraint type check failed for parameter '$name'
in Main (file src/, line 324)

Obviously the error message needs some work (and I would love to be able to generate custom error messages for subsets), but proper use of subsets are going to save a lot of code and prevent a lot of errors.

1 Comment

You can simply use multi for the error message.

One multi-call of foo uses the Filename type, another accepts Str. If it falls through to the string case, it didn't match the Filename subset.

Leave a comment

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book!