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:05
Subject:
Re: Things you can't do in a signatured sub
Message ID:
CAExogxPbUb_qK16gmkcc6mOu_gWMqSoEzM5LFk=PHgAtjgLrug@mail.gmail.com
2022-1-20 16:37 Darren Duncan <darren@darrenduncan.net> wrote:

>
> Well except for the common idiom of a combined getter/setter, which I
> personally
> don't like but it is common.  So in that case you would have in the new
> version
> of that like this:
>
>      sub myaccessor ($name, @maybe_new_value) {
>          if (scalar @maybe_new_value > 0)
>              $props->{$name} = $maybe_new_value[0];
>          }
>          return $props->{$name};
>      }
>
> -- Darren Duncan
>

You may assume the current accessor will be rewritten to the following code
using signatures.

  [Before]
  sub force {
    my $self = shift;
    if (@_ > 1) {
      $self->{force} = $_[0];
      return $self;
    }
    else {
      return $self->{force};
    }
  }

  [After]
  sub force ($self, $name, @value) {
    if (@value > 0) {
      $self->{force} = $value[0];
      return $self;
    }
    else {
      return $self->{force};
    }
  }

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