How can I determine the type of a blessed reference in Perl?

后端 未结 3 527
长情又很酷
长情又很酷 2020-12-30 01:31

In Perl, an object is just a reference to any of the basic Perl data types that has been blessed into a particular class. When you use the ref() function on an unblessed re

3条回答
  •  自闭症患者
    2020-12-30 01:54

    And my first thought on this was: "Objects in Perl are always hash refs, so what the hack?"

    But, Scalar::Util::reftype is the answer. Thanks for putting the question here.

    Here is a code snippet to prove this.. (in case it is of any use to anyone).

    $> perl -e 'use strict; use warnings "all";
                my $x = [1]; bless ($x, "ABC::Def");
                use Data::Dumper; print Dumper $x;
                print ref($x) . "\n";
                use Scalar::Util "reftype"; print reftype($x) . "\n"'`
    

    Output:

    $VAR1 = bless( [
                     1
                   ], 'ABC::Def' );
    ABC::Def
    ARRAY
    

提交回复
热议问题