in reply to Determining the true type of a reference

Maybe I'm just slow today. I don't even understand the question. You think that a reference of type ARRAY is the same as membership in the (poorly named) Class ARRAY? This seems nonsensical on it's face. Two entirely different things.

ref did what the doc says it does. So did isa.

I think the simple answer is "Don't name your classes after predefined types". Then you don't have this issue.

We have enough real porblems here without going out of our way to confuse ourselves.

Bob Niederman, http://bob-n.com
  • Comment on Re: Determining the true type of a reference

Replies are listed 'Best First'.
Re: Re: Determining the true type of a reference
by bobn (Chaplain) on May 25, 2003 at 19:14 UTC
    I'm not sure I actually responded to the real question. I still think naming a class 'ARRAY' is begging for trouble.

    But here a snippet that knows the difference:
    $aref = []; $href = {}; $sref = \""; my $text = 'some text'; my $ref = \$text; my $blessed = bless($ref, 'ARRAY'); print prt_typ($_) for ( [ $aref, '$aref'], [ $href, '$href'], [ $sref, '$sref'], [$blessed, '$blessed'], ); sub prt_typ { my ( $rin, $str ) = @{$_[0]}; print "ref($str) = ", ref($rin), "\n"; eval { my $x = ${$rin} }; return "$str is SCALAR\n\n" unless $@; eval { my %x = %{$rin} }; return "$str is HASH\n\n" unless $@; eval { my @x = @{$rin} }; return "$str is ARRAY\n\n" unless $@; }
    Results in:
    ref($aref) = ARRAY $aref is ARRAY ref($href) = HASH $href is HASH ref($sref) = SCALAR $sref is SCALAR ref($blessed) = ARRAY $blessed is SCALAR



    Bob Niederman, http://bob-n.com

      Unless we bring overloading in :-). Consider:

      { package FakeScalar; use overload '${}' => sub { \(shift->{scalar}) }; sub new { bless { scalar => undef }, shift }; }; use Scalar::Util qw(reftype); my $scalar = FakeScalar->new; print "reftype(FakeScalar) = ", reftype($scalar), "\n"; print prt_typ( [ $scalar => 'FakeScalar'] ); __END__ # produces reftype(FakeScalar) = HASH ref(FakeScalar) = FakeScalar FakeScalar is SCALAR
        OK, tyou're right.

        Now, tell me why you would do the vile, evil thing you did.

        ;-P



        Bob Niederman, http://bob-n.com