On 2020-01-21 22:44, Peter Pentchev wrote:
On Tue, Jan 21, 2020 at 07:16:17PM -0800, Todd Chester via perl6-users wrote:
On 2020-01-21 18:57, Tom Browder wrote:
On Tue, Jan 21, 2020 at 18:34 Todd Chester via perl6-users
<perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:
On 2020-01-21 16:09, Todd Chester via perl6-users wrote:
>> 4) A block (that is the { ... } bit) will always 'return' the last
>> expression evaluated.
>
> Seems to me I have see the last expression returned even without
> the {...}. Maybe I am misremembering.
Todd, the {} is the block defining the subroutine.
> sub AplusB( $a, $b --> Int ){$a+$b;}
The above is the sub's definition.
&AplusB
The above should generate an error because the mandatory args are
missing, depending on the context (don't quote me on that).
&AplusB is printer out by REPL. I don't know why. REPL
does that a lot.
The REPL tells you that the result of the expression you entered (the
sequence of characters "sub AplusB......{$a+$b}") was to define
a subroutine called "AplusB", which is what you wanted it to do.
The "&" sigil (a sigil is basically the character before a thing's name in
Raku and Perl 5) means that the name "AplusB" refers to a subroutine,
not a variable or anything else.
> AplusB 2, 3
5
The above shows the sub being called with the required two args, and the
5 is the returned value which is probably shown in the REPL since there
is no semicolon after the call and the 5 is not usually seen otherwise.
$ p6 'sub AplusB( $a, $b --> Int ){$a+$b}; say AplusB 2, 3;'
5
-Tom
Hi Tom,
I was trying to cut things down to the simplest terms.
I was responding to Richard's statement
>> 4) A block (that is the { ... } bit) will always 'return' the last
>> expression evaluated.
I created a sub without the "{...}" and showed where it would
return the last equation without the "{...}". I may have
misunderstood Richard.
You created a sub with "$a+$b" in a "{...}" block. The "{$a+$b}"
sequence of characters in what you typed is the "{...}" block that
Richard is referring to - a block that starts with a "{" character, ends
with a "}" character and contains a series of Raku expressions
separated by semicolons.
In your case the block only contains a single expression, "$a+$b", which
represents the sum of the two arguments to the subroutine - this is
*exactly* what Richard meant. If you were trying to illustrate his point
that a block without an explicit "return" statement would return
the last expression, you succeeded. If you were trying to demonstrate
something that does not have a "{...}", well, nope, "{$a+$b}" is exactly
the "{...}" that he was talking about, so the fact that AplusB returns
the sum of $a and $b is exactly what he meant.
G'luck,
Peter
Hi Peter,
What a fantastic piece of technical writing. Start
small and slowly move to the complicated. I understood
everything you said. (You have a history of that
with me.)
And the "sigil" explanation brought a grin to my
face. "He knows me!". And you were right, by the way.
I would not have remembered what it meant from my
Perl 5 days.
Now for someone who lives and breaths Top Down and
who drowns himself in subs and modules, I had to trace
back why I misunderstood Richard's
{...}
which of course it means
{body of the sub goes here}
I traced it back to NativeCall:
sub GetLastError() is native("Kernel32") is symbol("GetLastError")
returns DWORD { * };
To me
{ * }
means
{ I ain't telling you } or
{ some magic happens here }
And I confused it with {...}. And to add
injury, I thought is was a special quote,
such a reading a hash cell with $a = %h{$b}.
More magic goes here.
In your next life, you should consider being a teacher.
Thank you!
-T