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:
Alexander Hartmaier
Date:
January 20, 2022 09:20
Subject:
Re: Things you can't do in a signatured sub
Message ID:
CAB49QrbF0RwvJuk9d__kD7s+himNtE=kM4ECk5cj6_U6v3iPrg@mail.gmail.com
On Thu, Jan 20, 2022 at 7:13 AM Darren Duncan <darren@darrenduncan.net>
wrote:

> On 2022-01-19 9:32 p.m., Dan Book wrote:
> > On Thu, Jan 20, 2022 at 12:20 AM Darren Duncan 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)
>
> Okay, so in that case, I disagree with what Alexander said.  It sounds
> like
> there is nothing you can't do with signatures that you could do before.
> If you
> declare the signature with an array to capture miscellaneous extras, then
> problem doesn't exist.  Alexander, do you agree? -- Darren Duncan
>

Seems I have missed the possibility to do that in perlsub (
https://perldoc.perl.org/perlsub#Signatures). Maybe because the very first
example which shows this special case is a complex one using lvalue and its
variable is called @c. I'd suggest to start with a simpler example and have
a dedicated one for that case which calls it @rest. (variables 'at rest' ;-)

Thanks to all your input I've now converted all subs to signatured subs
adding @ to the sig of all undocumented methods, just to be sure to not
cause an exception.

As the docs are pretty long, sub-headings 😂 would help to quicker find the
correct example for ones use case.
If there is an agreement on this I'm willing to write patch.

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