in reply to How to tell if a variable is blessed ?

To tell you if the contents of the variable are a reference, and if so, the nature of that reference, use the function ref.
ref $var;
The other alternative is this:
eval { $ref->isa("Some::Package") };

Regards,
Edward

Replies are listed 'Best First'.
Re^2: How to tell if a variable is blessed ?
by linux454 (Pilgrim) on Mar 02, 2006 at 15:07 UTC
    How about a combination of the two?
    if ( ref($var) && $var->isa("Some::Package") ) { $var->someMethod(); }
    Or better yet:
    my $sub; if ( ref($var) && ($sub = $var->can("someMethod")) ) { $sub->(); } else { die "Method someMethod not found." }
    Of course you don't have to capture the return of "can" but it can be convenient.
Re^2: How to tell if a variable is blessed ?
by dragonchild (Archbishop) on Mar 02, 2006 at 15:42 UTC
    Better is
    eval { local $SIG{__DIE__}; $ref->isa('Some::Package'); }
    You might trigger a die handler, which could be bad. For example, one of Test::More's dependencies has one. See eval-blocks and Test::Builder for more on the topic.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?