Re: Can I call myself

2017-02-06 Thread ToddAndMargo

On 02/06/2017 08:32 AM, Larry Wall wrote:

On Sat, Feb 04, 2017 at 08:39:52PM -0800, ToddAndMargo wrote:
: Are there any special rules, like in Perl 5? Do I need to
: pre-declare the sub?
:
: sub BummerDude ($);
: sub BummerDude ($) { do something; }

For normal subs, you never have to predeclare, because the calling syntax can
assume an unrecognized function name just isn't defined yet, and waits till the
end to double-check that there really was a function of that name defined.

We don't do the equivalent of Perl 5's ($) prototype with normal subs, so you
don't accidentally fall into the situation of modifying the current grammar.
(Functions always assume they take a list of arguments, syntactically speaking.)
You can make a unary function, but you have to explicitly declare an operator:

 sub prefix: ($argh) { die $argh }

This does have to be declared before you can use it, but unlike in Perl 5,
you don't have to predeclare to use your new prefix recursively, because
it introduces the new operator the moment it sees it, even before it
parses the block that implements the operator.  (Perl 5 doesn't
introduce new syntax till the next statement, which is too late really,
which is why Perl 6 does it sooner.)  So if you want, you can write
factorial recursively like this:

 sub postfix:(Int $n) { if $n < 2 { 1 } else { $n * ($n - 1)! } }
 say 42!
 14050061177528798985431426062445115699363840

(Of course, that's not how we'd usually write factorial, since

 sub postfix:(Int $n) { [*] 2..$n }

is easier.)

Larry



Thank you!

--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: Can I call myself

2017-02-06 Thread Larry Wall
On Sat, Feb 04, 2017 at 08:39:52PM -0800, ToddAndMargo wrote:
: Are there any special rules, like in Perl 5? Do I need to
: pre-declare the sub?
: 
: sub BummerDude ($);
: sub BummerDude ($) { do something; }

For normal subs, you never have to predeclare, because the calling syntax can
assume an unrecognized function name just isn't defined yet, and waits till the
end to double-check that there really was a function of that name defined.

We don't do the equivalent of Perl 5's ($) prototype with normal subs, so you
don't accidentally fall into the situation of modifying the current grammar.
(Functions always assume they take a list of arguments, syntactically speaking.)
You can make a unary function, but you have to explicitly declare an operator:

sub prefix: ($argh) { die $argh }

This does have to be declared before you can use it, but unlike in Perl 5,
you don't have to predeclare to use your new prefix recursively, because
it introduces the new operator the moment it sees it, even before it
parses the block that implements the operator.  (Perl 5 doesn't
introduce new syntax till the next statement, which is too late really,
which is why Perl 6 does it sooner.)  So if you want, you can write
factorial recursively like this:

sub postfix:(Int $n) { if $n < 2 { 1 } else { $n * ($n - 1)! } }
say 42!
14050061177528798985431426062445115699363840

(Of course, that's not how we'd usually write factorial, since

sub postfix:(Int $n) { [*] 2..$n }

is easier.)

Larry


Re: Can I call myself

2017-02-04 Thread ToddAndMargo

On 02/04/2017 01:09 AM, Brent Laabs wrote:

I think you're looking for &?ROUTINE. &?BLOCK is also related.

https://docs.perl6.org/language/variables#index-entry-%26%3FROUTINE


Hi Brent,

Awesome reference on variables!  Thank you!  (I copied it down.)

-T

--
~~~
Having been erased,
The document you're seeking
Must now be retyped.
~~~


Re: Can I call myself

2017-02-04 Thread ToddAndMargo



On 2017-02-04 12:34 AM, ToddAndMargo wrote:

Hi All,

Just out of curiosity, in Perl 6 can a subroutine call itself?

-T

I am  fighting with a broken Net:FTP::rmdir in Perl 5 that
will not recuse as advertised (it is very intermittent).

And I can not use Net::Ftp in Perl 6 as it is hosed and
so is the Inline.

And it seems that Perl 5 doesn't like me calling myself,
which would do wonders for looping on rmdir until I get
everything.



On 02/04/2017 12:51 AM, Darren Duncan wrote:
Any decent programming language supports self-recursion, where a 
subroutine may invoke itself.  Perl 6 explicitly also supports this, 
and even has a special keyword for a routine to refer to itself 
without knowing its own name, especially useful for anonymous subs; I 
don't remember that keyword but it may have been something like "SUB" 
or "SELF". -- Darren Duncan


Are there any special rules, like in Perl 5? Do I need to pre-declare 
the sub?


sub BummerDude ($);
sub BummerDude ($) { do something; }

-T


Re: Can I call myself

2017-02-04 Thread Brent Laabs
I think you're looking for &?ROUTINE.  &?BLOCK is also related.

https://docs.perl6.org/language/variables#index-entry-%26%3FROUTINE

On Sat, Feb 4, 2017 at 12:51 AM, Darren Duncan 
wrote:

> Any decent programming language supports self-recursion, where a
> subroutine may invoke itself.  Perl 6 explicitly also supports this, and
> even has a special keyword for a routine to refer to itself without knowing
> its own name, especially useful for anonymous subs; I don't remember that
> keyword but it may have been something like "SUB" or "SELF". -- Darren
> Duncan
>
>
> On 2017-02-04 12:34 AM, ToddAndMargo wrote:
>
>> Hi All,
>>
>> Just out of curiosity, in Perl 6 can a subroutine call itself?
>>
>> -T
>>
>> I am  fighting with a broken Net:FTP::rmdir in Perl 5 that
>> will not recuse as advertised (it is very intermittent).
>>
>> And I can not use Net::Ftp in Perl 6 as it is hosed and
>> so is the Inline.
>>
>> And it seems that Perl 5 doesn't like me calling myself,
>> which would do wonders for looping on rmdir until I get
>> everything.
>>
>>


Re: Can I call myself

2017-02-04 Thread Darren Duncan
Any decent programming language supports self-recursion, where a subroutine may 
invoke itself.  Perl 6 explicitly also supports this, and even has a special 
keyword for a routine to refer to itself without knowing its own name, 
especially useful for anonymous subs; I don't remember that keyword but it may 
have been something like "SUB" or "SELF". -- Darren Duncan


On 2017-02-04 12:34 AM, ToddAndMargo wrote:

Hi All,

Just out of curiosity, in Perl 6 can a subroutine call itself?

-T

I am  fighting with a broken Net:FTP::rmdir in Perl 5 that
will not recuse as advertised (it is very intermittent).

And I can not use Net::Ftp in Perl 6 as it is hosed and
so is the Inline.

And it seems that Perl 5 doesn't like me calling myself,
which would do wonders for looping on rmdir until I get
everything.