Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seek Perl idiom to check that $self is a class or object

Tags:

oop

perl

In Perl, I just got bitten by something that looked like the bug below:

package Foo;
sub method {
    my $self = shift;
    my @args = @_;
    ...
}

where I called it as a subroutine, not a method:

Foo::method( "arg1", "arg2" );

rather than calling it as a method - in this case, it was a "class method":

Foo->method( "arg1", "arg2" );

Calling Foo::method("arg1","arg2") resulted in "arg1" getting dropped.

Similar considerations can arise with an "object method":

my $object = Foo->new();
$obj->method( "arg1", "arg2" );

Is there a friendly, concise, Perl idiom for checking that the first argument, conventionally called $self, is in fact an object in the class (package), and/or the class/package name?

The best I have come up with is:

package Foo;
sub method {
    my $self = ($_[0]->isa(__PACKAGE__) ? shift @_ : die "...error message...";
    my @args = @_;
    ...
}

which is not much more concise 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 dislike using experimental features.

I know about use attributes and :method. Is that the best way to go? Similar concerns about "evolving" features.

I know about Moose - but I don't think that Moose enforces this. (Did I miss anything.)

The problem with Perl is that there are so many ways to do something.

like image 906
Krazy Glew Avatar asked Feb 08 '16 23:02

Krazy Glew


2 Answers

The best answer is to not mix functions and methods in a single package. "Hybrid modules", as they're known, are problematic. Everything which you might want to make a function should instead be a class method call.

There should be little need to fully qualify a function call in day-to-day programming.


The most concise way is to use Moops which is the new way to use Moose with syntax-sugar.

use Moops;

class Foo {
    method something() {
        print("something called\n");
    }
}

Foo->new->something();
Foo::something();

# something called
# Invocant $self is required at /Users/schwern/tmp/test.plx line 10.

Moops is marked as unstable, but that's the interface, not the signatures themselves. Signatures have been around and usable in production for a long time, longer than they've been built in. More worrying is there hasn't been a release in over a year, however the author writes good stuff. Your call.


Otherwise, like with anything else, write a function.

use Carp;
use Scalar::Util qw(blessed);

sub check_invocant {
    my $thing = shift;

    my $caller = caller;

    if( !defined $thing ) {
        croak "The invocant is not defined";
    }
    elsif( !ref $thing ) {
        croak "The invocant is not a reference";
    }
    elsif( !blessed $thing ) {
        croak "The invocant is not an object";
    }
    elsif( !$thing->isa($caller) ) {
        croak "The invocant is not a subclass of $caller";
    }

    return $thing;
}

Since this returns the invocant and handles the exception for you it can be used very concisely.

package Foo;

sub method {
    my $self = ::check_invocant(shift);

    ...
}
like image 86
Schwern Avatar answered Oct 28 '22 02:10

Schwern


I'll add to what Schwern has written to say that you could also take a look at Safe::Isa, which lets you safely call isa on something which you cannot be sure is an object.

like image 33
oalders Avatar answered Oct 28 '22 01:10

oalders