in reply to Arrgh, can() appears to work for non-object !!

can and isa work on classes as well as objects. You are testing to see if the class fred has a can method, which it does since every class inherits from UNIVERSAL.

Scalar::Util::blessed can be your friend:

sub can_it { blessed($_[0]) && $_[0]->can('can') }

Replies are listed 'Best First'.
Re^2: Arrgh, can() appears to work for non-object !!
by Bloodnok (Vicar) on Jan 07, 2009 at 11:29 UTC
    TFT ysth,

    Yes, I know both can and isa work on both classes and objects, the thing is how can fred be a class ? Shouldn't I have expected a method can not found for class fred, did you forget to load fred ? type of error ?

    A user level that continues to overstate my experience :-))
      No, since nothing about a class is required. It need have no methods, @ISA, or anything else, so there's nothing missing to provoke a "forget to load" kind of error. Even the package statement isn't required, since you can declare anything using a fully qualified name if you want.
        Does that mean that it's safe to infer that perl will, if it thinks it appropriate, treat all literals/string constants as class names ?

        A user level that continues to overstate my experience :-))