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:
Yuki Kimoto
Date:
January 21, 2022 07:16
Subject:
Re: Things you can't do in a signatured sub
Message ID:
CAExogxP2OtCVGXhvD3WaQA-tG0ArF1gAi8=hYoQ8gXyGNvZSgg@mail.gmail.com
2022-1-20 16:15 Dan Book <grinnz@gmail.com> wrote:

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
>

If we want the number of arguments, we can make the array arguments with
signatures.

  [Before]
  sub foo {
    my $first = shift;
    if (@_ > 0) {
      # ...
    }
  }

  [After]
  sub foo ($first, @rest) {
    if (@rest > 0) {
      # ...
    }
  }

This is not so bad.

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