Perl 6: Junctions

| | Comments (0) | TrackBacks (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.

0 TrackBacks

Listed below are links to blogs that reference this entry: Perl 6: Junctions.

TrackBack URL for this entry: http://www.cattlegrid.info/mt/mt-tb.cgi/93

Leave a comment