2022-2-15 11:07 Yuki Kimoto <kimoto.yuki@gmail.com> wrote: > > 2022-2-15 10:50 Yuki Kimoto <kimoto.yuki@gmail.com> wrote: > >> >> 2022-2-15 2:12 Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> wrote: >> >>> >>> my ( $minargs, $maxargs ) = builtin::sub_arity $method; >>> $self->die( "Too few arguments for .$d directive" ) if 1+@args < >>> $minargs; >>> $self->die( "Too many arguments for .$d directive" ) if 1+@args > >>> $minargs; >>> >>> return $self->$method( @args ); >>> >>> Perl currently does not provide this. So I can't. >>> >>> >> If signatures enable arity checking, there is a strong need for the API >> to get the minimum and maximum number of arguments. >> >> This is because generally speaking, module authors know the details of >> Perl language, but module users don't know the details. >> >> > Additional Description: > > In this way, @_ can be suppressed because the definition of a signature is > determined at compile time; > > Callbacks can receive the correct arguments. > > $cb->(splice @args, 0, $maxargs); > > > Additional Description 2: If arity checking is enabled, and it is included to v5.36 # End user(test.pl) use v5.36; use Foo; Foo->execute(sub ($foo) { # ... }); # The module author (This module supports Perl 5.10) package Foo; sub execute { my ($cb) = @_; $cb->(1); } The module author adds an argument after a year. package Foo; sub execute { my ($cb) = @_; # Adds an argument $cb->(1, 2); } The "test.pl" crashes by the message "Too many arguments". The "test.pl" maybe a production code. In order that the module author pass arguments safely, dual life module " buildin.pm" and the function to pass arguments safely. # Pass arguments safely package Foo; use builtin 'call_cb_safe'; sub execute { my ($cb) = @_; # Pass arguments safely call_cb_safe($cb, (1, 2)); } The "call_cb_safe" passes the safe arguments to the callback by seeing the definition of the signatures of the callback. If the callback doesn't have signatures, all arguments are passed.Thread Previous | Thread Next