Perl 6: Junctions

| | Comments (0)

I recently began playing a bit with pugs, the current implementation of Perl 6. Junctions seem to be a great feature: among other things, they allow to write much cleaner or conditionals. Here's some Perl 5 code:

#!/usr/bin/perl

for my $num (1..8) {
    if ( $num == 3 || $num == 4 || $num == 6 ) {
        print "$num is ok!\n";
    }
}

and the corresponding Perl 6:

#!/usr/bin/pugs

for 1..8 -> $num {
    if ( $num == 3|4|6 ) {
        say "$num is ok!";
    }
}

The junction can also be created from an array:

#!/usr/bin/pugs

my @mylist = <3 4 6>;

for 1..8 -> $num {
    if ( $num == any(@mylist) ) {
        say "$num is ok!";
    }
}

Ahhh, syntactic sugar... :-) And you can have some it even in Perl 5 with the (lightweight) module Perl6::Junction.

Leave a comment

About this Entry

This page contains a single entry by Michele Beltrame published on June 12, 2007 10:56 PM.

Catalyst apparel & houseware was the previous entry in this blog.

A bowl of food for an homeless animal is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Powered by Movable Type 4.23-en