in reply to Re: Re: Re: Class::Interface -- isa() Considered Harmful
in thread Class::Interface -- isa() Considered Harmful

No, I am very much not looking for Scalar::Util::reftype, thought it could indeed replace UNIVERSAL::isa. It still dictates implementation, which is what I'm trying to avoid.

I'm not expecting to rummage around in the internals of an object passed as $args. I'm expecting to deal with something that behaves as a hash. If it's a hash, fine. If it's a tied hash, it ought to work fine too.

Unfortunately, it doesn't:

#!/usr/bin/perl -w use strict; use Scalar::Util 'reftype'; use Tie::Hash; foreach my $package (qw( Scalar Array Code )) { my $h = tie my %new_hash, $package . 'Hash'; print "Tied $h as a hash based on $package\n"; print 'It is ' . (UNIVERSAL::isa( $h, 'HASH' ) ? '' : 'not ') . "a + HASH\n"; print 'Its reftype is ' . reftype( $h ) . "\n"; } package ScalarHash; sub TIEHASH { bless \(my $foo), $_[0] } package ArrayHash; sub TIEHASH { bless [], $_[0] } package CodeHash; sub TIEHASH { bless sub {}, $_[0] }

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Class::Interface -- isa() Considered Harmful
by MarkM (Curate) on Jan 17, 2003 at 23:38 UTC

    In your example, $h is not a tied hash. $h is a blessed reference to an object that is used to store data for the tied hash. The tied hash is %new_hash.

    For a better example than the one you are presenting:

    use Scalar::Util qw(reftype); tie(%hash, "ScalarHash"); function_that_expects_a_hash_reference(\%hash); sub function_that_expects_a_hash_reference { my($hash_ref) = @_; reftype($hash_ref) eq 'HASH' or die "CALLED WITHOUT A HASH REFERENCE!\n"; ... $hash_ref->{...} ... } package ScalarHash; sub TIEHASH { bless \(my $foo), $_[0] }

    As I pointed out in the node that you were responding to, you are confusing Perl object interfaces (the ability to use a scalar as if it were a hash reference), and Perl blessed reference methods (the ability to use a particular method with an object).

      Thanks; that particular behavior of tying had always confused me. You're right -- Scalar::Util::reftype() does what I want in this example.