in reply to Moose: get class name

Does not ref $self work, just like for regular perl objects?

sub role_action { my $self = shift; my $obj_type = ref $self; # ... }
I don't think this is any different. __PACKAGE__ never tells you the class name for the object you're working with, it only gives you the package that you're currently in, which is not the same thing at all if you're in a package that can be inherited from.

Update: the difference between ref and blessed on $self is probably moot since if $self isn't a reference to a blessed object, either you won't be here or you're probably about to blow up badly anyway. For objects other than $self, there's a bigger difference.

Replies are listed 'Best First'.
Re^2: Moose: get class name
by kcott (Archbishop) on Nov 10, 2010 at 18:00 UTC

    The difference between ref and Scalar::Util::blessed is subtle and, as you say, won't be any different when operating on a blessed reference.

    blessed is exported as a method, so you can say $moose_object->blessed(), which was why I pointed to that. You can achieve the same with ref($moose_object).

    Having said that, I think I'd prefer:

    $moose_object->blessed()->some_class_method()

    to

    ref($moose_object)->some_class_method()

    rootcho gave no indication of how the class name was to be used, so that may not matter.

    -- Ken