Dan writes "This one's about subroutines and is rather extensive. Read it over at perl.com." Yay! It's been a long time coming, it should be extensive.
Re:Could be...
RobertX on 2003-03-11T00:40:28
Any my goodness what the heck happened to subs! Talk about simplicity in any sense being thrown out the window.Re:Could be...
TimToady on 2003-03-11T01:29:22
No, the simplicity was never there in the first place, if you count all the circumlocutions people had to go through to work around Perl's limitations. There was an impedance mismatch between the problem space and the solution space. Perl 6 attempts to factor out all that complexity from user code, and for the price of a more complex declaration, you get a much simpler subroutine. It's a net win.Re:Could be...
Damian on 2003-03-11T02:51:16
my goodness what the heck happened to subs!
We fixed them.
Talk about simplicity in any sense being thrown out the window.
Hardly. If you want to go on writing "simple", Perl 5 style subs:
# Perl 6
sub foo {
my ($value, @subrefs) = @_;
return map &{$_}($value), @subrefs;
}
print foo(7, @subs);
you still can. Indeed, that sub will run identically under both Perl 5 and Perl 6.
No extra understanding required.
However, we're also enriching Perl's subroutine definition mechanism, adding the ability to do the same thing far more efficiently and safely (but only if you want to):
# Perl 6
sub foo (Int $value, *@subrefs of Code(Int)) {
return map $_($value), @subrefs;
}
print foo(7, @subs);
And we're giving you optional mechanisms to handle more complex cases -- like named flags -- more cleanly too. For example:
# Perl 6
sub foo (Int $value, *@subrefs of Code(Int), +$max = Inf, *%flag, ) {
return (%flag{rev} ?? reverse:: *)
<== (%flag{sort} ?? sort:: *)
<== map $_($value), @subrefs[0..$max];
}
print foo(7, max=>4, rev=>1, @subs);
as opposed to:
# Perl 5
sub foo {
my ($value, @args) = @_;
my %flag = (max=>$#args);
while ($args[0] =~/^(rev|sort|max)$/) {
$flag{$1} = (splice @args, 0, 2)[1];
}
my @result = map &{$_}($value), @args[0..$max];
@result = sort @result if $flag{sort};
@result = reverse @result if $flag{rev};
return @result;
}
print foo(7, max=>4, rev=>1, @subs);
I guess it depends what you mean by "simplicity".Re:Could be...
RobertX on 2003-03-11T10:40:25
Now see I understood that more than the whole Wall article! I will just keep plugging along in my learning and maybe I will be ready by the time P6 comes out. : )Re:Could be...
2shortplanks on 2003-03-11T11:08:45
Ah, the key to any Perl Apocalypse is too not get your nogin in a twist too soon, and wait for Damian to explain it all in an Exegesis. After all, tutorials are much easier to understand than specifications....Re:Could be...
RobertX on 2003-03-11T12:11:58
Amen!Or, as we used to say in Rabbinical school...
yudel on 2003-03-11T14:04:26
Exegesis love you!Re:Could be...
babbage on 2003-03-11T15:52:24
Also, keep in mind that these specifications are meant to highlight the differences that will be in Perl6, not the similarities. A lot of these new constructs are just there if you want to use them, but if you want to keep writing Perl5 style code, in most cases you will be able to do so without hassle -- and, of course, there will be a Perl5 mode beyond that, but for the moment I'm just talking about "native" Perl6. If you want your Perl6 to look essentially like Perl5, that will be a-okay.<hhgttg> Don't panic! </hhgttg>
Re:Could be...
jhi on 2003-03-12T13:19:45
The good old hammer is still in your toolbox. You were just given a Dremel, too.
Re:compatible psiglets
Damian on 2003-03-11T20:26:55
# assuming a Bovine isa Mammal, but not necessarily vice versa
In fact: necessarily not vice versa. No inheritance loops allowed!;-)
my &moo(Mammal)::= sub (Bovine $x) { $x.speak };
Error. You're going to be allowed to pass any kind of Mammal to moo, but there's no guarantee it will be a Bovine as the anonymous sub requires.
my &moo(Bovine)::= sub (Mammal $x) { $x.speak };
Fine. You're going to be allowed to pass any kind of Bovine to moo, and so it's certain that it will also be a Mammal.Re:compatible psiglets
wickline on 2003-03-12T20:37:35
> Error. You're going to be allowed to pass any kind of Mammal to moo
I knew it would be a programmer error. I was wondering if it would be a runtime error or compile time error as well. From what you've said it sounds like it'll be a runtime error if one ever didThanksmy &moo(Mammal)::= sub (Bovine $x) { $x.speak };
moo( $some_non_bovine_mammal );:)
-mattRe:compatible psiglets
Damian on 2003-03-12T21:52:48
The binding is compile-time (::=) so the compiler can determine that the signatures are incompatible. So it would likely be a compile-time error.