In Perl, I just bit something like the error below:
package Foo; sub method { my $self = shift; my @args = @_; ... }
where I called it a subroutine, not a method:
Foo::method( "arg1", "arg2" );
instead of calling it as a method - in this case it was a "class method":
Foo->method( "arg1", "arg2" );
A call to Foo :: method ("arg1", "arg2") caused the "arg1" to be discarded.
Similar considerations may arise with the "object method":
my $object = Foo->new(); $obj->method( "arg1", "arg2" );
Is there a friendly, concise idiom of Perl to verify that the first argument, conditionally called $self , is actually an object in the class (package) and / or class / package name?
The best I came up with is:
package Foo; sub method { my $self = ($_[0]->isa(__PACKAGE__) ? shift @_ : die "...error message..."; my @args = @_; ... }
which is not much shorter than
package Foo; sub method { my $self = shift; die "...error message..." if $self->isa(__PACKAGE__); my @args = @_; ... }
or
package Foo; use Carp::Assert; sub method { my $self = shift; assert($self->isa(__PACKAGE__)); my @args = @_; ... }
Notes:
I know about Perl signatures, but I don't like to use experimental functions.
I know about use attributes and :method . Is this the best way to go? Similar concerns about “evolving” features.
I know about Musa - but I don’t think that Elk is imposing it. (Did I miss something.)
The problem with Perl is that there are so many ways to do something.