develooper Front page | perl.perl5.porters | Postings from January 2022

Re: Things you can't do in a signatured sub

Thread Previous | Thread Next
From:
Dan Book
Date:
January 20, 2022 07:16
Subject:
Re: Things you can't do in a signatured sub
Message ID:
CABMkAVUMdfHtBH-Ra2=8o_3Bna9DbPKzS5jNjisHNRjWvXxq8w@mail.gmail.com
On Thu, Jan 20, 2022 at 2:08 AM Yuki Kimoto <kimoto.yuki@gmail.com> wrote:

>
>
> 2022-1-20 14:32 Dan Book <grinnz@gmail.com> wrote:
>
>> On Thu, Jan 20, 2022 at 12:20 AM Darren Duncan <darren@darrenduncan.net>
>> wrote:
>>
>>> On 2022-01-19 12:43 p.m., Alexander Hartmaier wrote:
>>> > I have one sad example to add: defining a sub that is passed as a
>>> callback to a
>>> > module which doesn't document the (number of) arguments and call it in
>>> different
>>> > parts with a different number of arguments. Example I'm just dealing
>>> with is
>>> > Mojo::RabbitMQ::Client.
>>> > In the end I gave up and didn't specify a signature to avoid
>>> exceptions caused
>>> > by the different number of arguments.
>>>
>>> So here's a question.  The way signatures work now, if one puts "@" at
>>> the end
>>> of the signature, does that say silently accept but throw away the extra
>>> arguments, or is there some way in a signature to say explicitly that
>>> you take a
>>> variable number of arguments and there is an array you can get "the
>>> rest" in?
>>
>>
>> ($arg, @rest)
>>
>> -Dan
>>
>
> Do you recommend the following code to the Perl users?
>
>   sub foo ($arg, @rest)  {
>     my ($foo, $bar) = @rest;
>   }
>
> Instead of
>
>   sub foo ($arg, $foo, $bar)  {
>
>   }
>
>
>
For the cases specified in this thread, yes:

* if you want to allow for more arguments to be provided or added to the
API at a later date
* if you want to count the number of arguments provided
* if you want to conveniently pass the set of arguments on to another
function

-Dan

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About