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

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

Replies are listed 'Best First'.
Re: Re^3: Determining the true type of a reference
by bobn (Chaplain) on May 26, 2003 at 01:44 UTC
    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.