in reply to How to differentiate hash reference and object reference?

From perlfunc:
ref EXPR
ref


Returns a true value if EXPR is a reference, false otherwise. If EXPR is not specified, $_ will be used. The value returned depends on the type of thing the reference is a reference to. Builtin types include:

SCALAR
ARRAY
HASH
CODE
REF
GLOB
LVALUE

If the referenced object has been blessed into a package, then that package name is returned instead. You can think of ref as a typeof operator.
So, there's no need to call ->isa():
sub wants_foo_object { my $obj = shift; die unless $obj && ref($obj) eq "foo"; #... }
(untested)


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: How to differentiate hash reference and object reference?
by phaylon (Curate) on Aug 31, 2005 at 12:51 UTC
    But ->isa return's true if the argument is in the package's @ISA array. ref( $obj ) does something different. I think UNIVERSAL may be more the way to go.

    Ordinary morality is for ordinary people. -- Aleister Crowley

      No, isa() returns true if whatever method isa() is for the invocant it returns true. If I wrote an object and overrode isa() to return true based on a the phase of the moon, that's what it would do.

      That's probably unwise code to write, but the purpose of having isa() be a method is so that classes can override it when it makes sense to do so.

        Yes, but I was more on to what the OP wanted to achieve with it. Therefore the suggestion of UNIVERSAL::isa.

        Ordinary morality is for ordinary people. -- Aleister Crowley