Perl 6 : Not Just For Damians
by Piers Cawley
|
Pages: 1, 2, 3, 4
"Apocalypse 3 is mostly mere syntactic sugar"
You say that like it's a bad thing. Perl's creed has always been to make the easy things easy and the hard things possible. In many ways, Perl 6 is going further than that: making hard things easy. And Apocalypse 3 continues this trend.
Well chosen syntactic sugar is good voodoo. It's Laziness with a capital L, and Laziness, as we all know, is a virtue.
Let's look at what we get in Apocalypse 3.
The hyper operator.
Well, this is just a foreach loop isn't it? Yes, but as Damian
subsequently pointed out, would you rather write and maintain:
my @relative;
my $end = @max > @min ? @max > @mean ? $#max : $#mean
: @min > @mean ? $#min : $#mean;
foreach my $i ( 0 .. $end ) {
$relative[$i] = $mean[$i]) / ($max[$i] - $min[$i]);
}
or
my @relative = @mean ^/ (@max ^- @min);
In the second case, the intent of the code is clear. In the first it's obfuscated by the loop structure and set up code.
Chainable file test ops
Why hasn't it always worked like this? Ah yes, because the internals of perl 5 wouldn't allow for it. This is an example of the far reaching effects of some of the earlier Apocalypses giving us cool stuff.
Let's do the comparison again.
Perl 5:
my @writable_files = grep {-f $_ && -r _ && -w _ && -x _} @files;
Perl 6:
my @writable_files = grep { -r -w -x -f $_ } @files;
Shorter and clearer. Huzzah.
Binary ';'
This one's a bit odd. We've not yet seen half of what's going to be done with it, but I have the feeling that the multidimensional array mongers are going to have a field day.
=> is a pair builder
Mmmm... pairs. Lisp flashbacks! Well, yes. But if hashes are to become 'bags' of pairs, then it seems that hash keys won't be restricted to being simple strings. Which is brilliant. On more than several occasions I've found myself wanting to do something along the lines of
$hash{$object}++;
and then later do
@results = map { $_.some_method }
grep { $hash{$_} > 1 }
keys %hash;
Try doing that in Perl 5.
The use of pairs for named arguments to subroutines looks neat too, and should avoid the tedious hash setup that goes at the top of any subroutine that's going to accept named parameters in Perl 5.
Lazy lists
Lazy lists are cool, though I note that Damian couldn't squeeze a compelling example of their usage in Exegesis 3. For some applications they are a better mousetrap, and if you don't actually need them they're not going to get in your way. I'm not sure if Larry has confirmed it yet, but I do like the idea of being able to do:
my($first,$second,$third) = grep is_prime($_), 2 .. Inf;
and have things stop when the first three primes have been found.
And having
my($has_matches) = grep /.../ @big_long_list;
stop at the first match would be even better.
Logical operators propagating context
Where's the downside? This can only be a good thing. Multiway
comparison 0 < $var <= 10 is another example of unalloyed
goodness.
Backtracking operators
I'm not entirely sure I understand this yet. But it looks like it has the potential to be a remarkably powerful way of programming (just like regular expressions are, which do loads of backtracking). I have the feeling that parser writers are going to love this, and equation solvers, and...
But again, if you don't need the functionality, don't use it. It'll stay out of your way.

