But I suppose you don't want the sub missing() to be available as method the way ->foo() is?
And while the MISSING object is a nice workaround for the semi-predicate problem, I still think it's an overkill here.
Like I said you could just slurp into @val and have the full functionality, with less code:
use strict; use warnings; use experimental "signatures"; use Carp; { package BLA; sub new ($class,@properties) { return bless {@properties}, $class; } sub foo ($self, @val) { unless (@val){ # getter return $self->{foo} } elsif (@val == 1) { # setter return $self->{foo} = $val[0]; } else { # ERROR local $" = ","; Carp::croak "Too many arguments for method 'BLA->foo(@val) +'"; } } } my $x = BLA->new(foo=>42); warn "getter:", $x->foo; warn "setter:", $x->foo(666); warn "getter:", $x->foo; warn "error:", $x->foo(42,666);
-*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Fri Jan 8 21:39:32 C:/Perl_524/bin\perl.exe -w d:/tmp/pm/missing_param.pl getter:42 at d:/tmp/pm/missing_param.pl line 31. setter:666 at d:/tmp/pm/missing_param.pl line 32. getter:666 at d:/tmp/pm/missing_param.pl line 33. Too many arguments for method 'BLA->foo(42,666)' at d:/tmp/pm/missing_ +param.pl line 34. Compilation exited abnormally with code 255 at Fri Jan 8 21:39:33
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
improved code with croak and better error message
In reply to Re^2: Distinguish between missing and undefined arguments with subroutine signatures
by LanX
in thread Distinguish between missing and undefined arguments with subroutine signatures
by jo37
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |