Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yep very scrummy! And its also been available (for methods) in Perl5 for a while as well: http://news.ycombinator.com/item?id=1040614

Here is the perl6 example using MooseX::Declare (http://search.cpan.org/dist/MooseX-Declare/) in perl5:

    use 5.012;
    use MooseX::Declare;

    class Foo {
        use MooseX::Types -declare => [ qw(Even) ];
        use MooseX::Types::Moose qw(Int);

        BEGIN {
            subtype Even, as Int, where { $_ % 2 == 0 };
        }

        method bar ( Even $a ) { say $a }
    }

    my $foo = Foo->new;

    $foo->bar( 2 );  # => prints 2
    $foo->bar( 3 );  # => throws error


This is a cool capability. But I have to admit, it's lacking in the syntactic sugar that made the previous example look so appealing. I just simply read the code out loud in English and immediately knew what it meant. This is a bit more of a tongue twister. This notion of defining subsets of symbols like this could make code correctness for many cases a trivial thing.


Well, can you reasonably expect much else? It's a backported feature from perl6, constrained by the existing syntax of perl5. Of course it won't have the syntatic sugar.

One of the other cool capabilities of perl6 is the rules system, expanding on regexs so you can work with full on grammars. The perl6 grammar, written in perl6:

http://svn.pugscode.org/pugs/src/perl6/STD.pm6


No, I'm simply stating that I really like what they're doing with Perl 6 so far. I'm excited by the new grammar system immensely.

It's clear that doing it in an older syntax is clearly less elegant. However, it's very impressive though that the back ports of Perl 6 features to Perl 5 are proceeding relatively nicely though. The work on Perl 6 has definitely been a boon to 5.


The BEGIN { subtype ... } code is just pure perl/Moose. So its possible for the MooseX::Declare developers to add some extra sugar there.


It goes away as soon as you put the MooseX::Types declaration into its own package. Which one does pretty often, since reusing types and coercions produces more maintainable code in general. The title of that library isn't "Organise your Moose types in libraries" without reason :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: