And using Moose is generally good.
So, I want to test if something is an object. I am using Moose.
1) Create a large hash to compare keys against, and then do
my $not_object_ref = {
HASH => 1, ARRAY => 1, GLOB => 1,...
};
my $is_object;
if ( my $ref = ref $object ) {
if ( ! $not_object_ref->{$ref} ) {
$is_object++;
}
}
# stuff that uses boolean value of $is_object
2) Use a Moose Attribute and eval
has q{_i_am_an_object} => (
isa => q{Object},
is => q{rw},
);
sub _is_object {
my ( $self, $object ) = @_;
my $is_object = 0;
eval {
$self->_i_am_an_object( $object ); # test if this is an object
$is_object++;
} or do {}; # I like PBP and perl critic :)
return $is_object;
}
my $is_object = $self->_is_object( $object );
# stuff that uses boolean value of $is_object
I don't know if this is a pure abuse of the Moose Attribute system, or if there is a much neater way of doing it, but certainly it has merit over the whole keeping track of what refs are not objects. And certainly, if you want to check against the object type, you change the isa to the class name, and give it a better method name.
It's probably more of an abuse of eval :)
Andy