Don't ask to ask, just ask | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
All of this is laid out here, and this particular item is in the invocant parameter section. But let me see if I can explain it for you. Invocant parameters allow you to associate an argument* (usually the first one) with a subroutine. In a very round about way, you could view this as dynamically adding a method to the class of the invocant, however that is not really what is happening (although it is kind of what it looks like). The synopsis describes the invocant parameter syntax as having a colon after the invocant parameter. However, the colon is apparently implied with all subs. So this code: is actually equivalent to this code: Which will allow it to be called "hello world".foo. Still another way to view this would be that it's Reverse Polish Notation perl ;) To be honest, I am not sure I like this feature 100% myself. It does give things a weird look, and you are correct in that it seems to be OO, when its not OO. However, it is worth keeping in mind that AFAIK, all types in perl 6 will be objects, and that the runtime will perform all the neccessary autoboxing DWIMery. So even if you are not doing OO, you are using OO. Now as for the *$x, *@xs code, I can answer that pretty easily. When you apply a * to a parameter, it becomes slurrpy. Since perl 6 has named parameters, argument lists don't flatten as they do in perl 5. So in order to do the common perl trick of slurping all the args into an array or hash, they introduced this syntax. This is discussed in the "Flattening argument lists" section of Synopsis 06. In that section they show how you can force a list to be flattened when you pass it into a subroutine. In the quicksort code, it is doing the reverse, and saying that it should slurp up anything that is passed into it. First the *$x slurps up a scalar (which is just one element). This is not unlike just saying $x although they are not interchangable (don't as my why, I am still trying to grok details like this myself). The second parameter *@xs then slurps up a array, which in effect slurps up the rest of the arguments. Again, remember that perl 6 has formal parameters, and that is very different from So it is best not to view them as even remotely equivalent. I hope this helps (and not hurts) as I am still trying to digest much of this stuff myself. * Actually the argument's type since you cannot and would not want to bind to a specific literal argument itself
-stvn
In reply to Re: Perl6 dot syntax (for methods?)
by stvn
|
|