How to get blissful ref type? (ARRAY | HASH | SCALAR)

They gave me the link, and I want to know its type. To this end, "ref" works on indisputable links, but on blessed links it returns the package name was blessed.

$a=[]; print ref $a; 

ARRAY

  bless $a, 'mytype'; print ref $a; 

Mytype

How can I get the type?

+4
source share
3 answers
 use Scalar::Util 'reftype'; print reftype bless {}; # HASH 
+14
source

You can use Scalar :: Util :: reftype for this, but overall this is not what you need to do. Typically, people asking this question indicate that they are doing something even less than ideal. So why do you think you need to know?

+7
source

As stated in the comment above .. you are mistaken if you think that the external code should be aware of the details of the implementation of the blessed object. If you need to do something like Data :: Dumper for an object, create a dump () method in a module that contains the implementation logic. You can add some code to the UNIVERSAL class autoloader so that it simply calls Data :: Dumper if you try to call dump () on an object that does not define it.

0
source

Source: https://habr.com/ru/post/1285987/


All Articles