in reply to Re: Determining the true type of a reference
in thread Determining the true type of a reference

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

Replies are listed 'Best First'.
Re^3: Determining the true type of a reference
by adrianh (Chancellor) on May 25, 2003 at 23:13 UTC

    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
        Now, tell me why you would do the vile, evil thing you did.

        :-)

        I might do it when you have an object that can usefully be treated as another perl type. For example consider an class like this:

        my $account = Account->new(; $account->add_transaction(30, 'food')->add_transaction(3, 'comics') ->add_transaction(99, 'food'); # get the second transaction my $transaction = $account->[1]; # get all the food transactions my $array_ref = $account->{food};

        I freely admit there are other APIs that would allow you to do the same thing. You could also implement the above with tie instead of overloading. However, overloading is not a totally insane solution if you just need read access.