Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #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:

sub foo ($x) { say($x) }
is actually equivalent to this code:
sub foo ($x:) { say($x) }
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.

sub foo($x, $y, $z) {...} # expects three scalars @onetothree = 1..3; # array stores three scalars foo(1,2,3); # okay: three args found foo(@onetothree); # error: only one arg foo(*@onetothree); # okay: @onetothree flattened to thr +ee args
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
sub perl6foo ($bar, $baz) { }
is very different from
sub perl5foo { my ($bar, $baz) = @_; }
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

In reply to Re: Perl6 dot syntax (for methods?) by stvn
in thread Perl6 dot syntax (for methods?) by Thilosophy

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2019-10-14 08:28 GMT
    Find Nodes?
      Voting Booth?