Diving into Perl 6


I'm officially diving into Perl 6.

I wrote my first real Perl 6 from scratch today. It's a prime number tester, where we test both via checking factors iteratively, and also using the magic prime number tester regular expression. I was inspired by a blog post about how PHP can't always handle regexes with lots of backtracking, and I thought it would make a good stress for Perl 6.

My program as it stands now looks like this:


use v6;

# Perl 5 @ARGV is now @*ARGS
my Int @candidates;
if ( @*ARGS ) {
    for @*ARGS -> $n {
        push @candidates, $n.Int;
else {
    @candidates = 2 .. 100;

for @candidates -> Int $x {
    my $via_factors = is_prime_via_factors($x);
    my $via_regex   = is_prime_via_regex($x);

    if $via_factors && $via_regex {
        say "$x is prime";
    elsif $via_factors xor $via_regex {
        say "Difference in opinion on $x :",
            "factors says $via_factors, regex says $via_regex";

sub is_prime_via_factors( Int $n ) returns Bool {
    my $top = sqrt $n;

    for 2..$top -> $i {
        return False if $n %% $i;
        # %% is the divisible-by operator
    # Could also use:  $n %% none(2 .. $top)

    return True;

sub is_prime_via_regex( Int $n ) returns Bool {
    my $str = 'x' x $n;

    # First capture is $0, not $1 or \1 as in Perl 5
    return False if $str ~~ regex { ^ x $ | ^ (xx+?) $0+ $ };

    return True;

I'm sure there are ways that are more Perl 6ish to do what I've done above, but I was glad to learn along the way. Big thanks to the #perl6 IRC channel for help. Some important lessons:

  • Command-line args are in @*ARGS, not @ARGV.
  • The old chestnut ($x mod $y == 0) is now ($x %% $y) with the %% divisible-by operator.
  • The first capture group in a regex is $0, not $1 or \1 as in Perl 5.

There's a document about the differences between Perl 5 and Perl 6 which I updated based on tonight's fun, and will keep adding to along the way.

My goal is to have all of perl101.org include examples in both Perl 5 and Perl 6, both for people new to Perl and for those moving to Perl 6. If you'd like to help in this effort, let me know or work on the perl101 github project.


I think it's a great idea to update the perl101 stuff, but I'd strongly recommend not publishing the actual changes till rakudo star (or whatever release) is ready for more than just early adopters. We don't want noobs being like, "Oh hey, this is a new version, I'll use this instead!" And then being mad that it's a billion times slower (which of course we know and expect.)

There is already a pretty good open-doc seed on wikibooks: http://en.wikibooks.org/wiki/Perl_6_Programming

I don't know why Perl6 should regress to the non-wiki format. Seeding the tutorial mess that is perl5 in perl6 sounds like a certifiably horrible idea.

@*ARGS ... Really? WTFBBQ! I don't follow Perl 6 very closely, so this was a new one on me. I recall reading they changed ARGV to ARGS to improve readability or whatever (even if it was a bit gratuitous, I can at least understand that), but the asterisk blows that rationale out of the water. I just don't get the Perl 6 developers.

The * twigil denotes @*ARGS as a contextual variable. This allows well-encapsulated dynamic scoping for certain values which need something more complex than nested lexical scopes but which don't warrant process-global scope.

In other words, the variable name itself reveals important information about the intent of its value and scope and use (in a similar way to the leading sigil).

As a long time perl hacker. Do people still think perl6 will be an overall net benefit to the community? It's syntax has changed so drastically it would be better launched as an entirely new language, a somewhat related to a perlish derivative.

Worst case perl6 fractures the perl community into two camps. Best case, perl6 creates a new and separate community that evolves side by side perl5. I can't foresee a smooth'ish transition like php3 -> php4 -> php5 made.

Sorry for all the negativity. Hope I'm wrong.

What makes you think Perl 5 is the sole holder of the term "Perl" for now and for good?

And isn't it amazing that Perl 6 has native support for multiple dispatch?

I don't think perl5 == "Perl" for good. Typically I'm used to when you increment the major version to the next integer you have a very similar base with new features and extensions. For instance I would love to see Moose be integrated into the core of perl and rev the version.

Perl 6 might be really cool, however it's very different from its predecessor. I suppose I agree with Andy that Larry can put the label Perl 6 on whatever he wants. But it might as well be call LarryLang 1.

I'm sure the active perl6 hacker have been hearing this for years. And it must get really old, tiresome and unproductive. Plus all this does not even matter this ship has sailed. For brand new projects perl 6 has some major benefits, distributed computing comes to mind immediately. I just can't see myself porting over the stuff I have in perl5 until there is a good reliable CPAN tool chain.

I commented some more in Andy new thread on this subject.

Leave a comment

Job hunting for programmers

Land the Tech Job You Love, Andy Lester's guide to job hunting for programmers and other technical professionals, is available in PDF, ePub and .mobi formats, all DRM-free, as well as good old-fashioned paper.