Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting classname of a object in Perl

Tags:

perl

I have an object reference and it could be a reference to an object of type 'FooInvalidResults'

There is a file called FooInvalidResults.pm and there is a line 'package FooInvalidResults' in it.

will the following work?

my $class = blessed $result;
if ($class eq 'FooInvalidResults') {
  # do something
} else {
  # do something else
}
like image 524
user855 Avatar asked Sep 26 '11 13:09

user855


3 Answers

It is also possible to do the job with the ref() builtin rather than Scalar::Util::blessed():

$ perl -E '$ref = {}; bless $ref => "Foo"; say ref $ref'
Foo

Note that if the reference is not blessed, this will return the type of reference:

$ perl -E '$ref = {}; say ref $ref'
HASH

However, as others have mentioned, UNIVERSAL::isa is a better solution.

like image 50
frezik Avatar answered Nov 07 '22 18:11

frezik


String-comparing class names is generally a bad idea, because it breaks polymorphism based on subtypes, and because it's generally not good OO practice to be that nosy about intimate details of an object like its exact package name.

Instead, write $result->isa('FooInvalidResults') — or, if you're paranoid about the possibility that $result isn't an object at all, blessed $result && $result->isa('FooInvalidResults').

Using UNIVERSAL::isa is a bad idea because some objects (for instance, mock objects for testing) have legitimate reasons to override the isa method, and calling UNIVERSAL::isa breaks that.

like image 40
hobbs Avatar answered Nov 07 '22 19:11

hobbs


Why didn't you use UNIVERSAL::isa?

if UNIVERSAL::isa( $result, "FooInvalidResults" ) {
   ...
}

This was bad advice, please use

$obj->isa( 'FooInvalidResults' );

I wasn't full aware of the difference between subroutine call ( BAD ) and method call ( GOOD ), but it became clear after I did some RTFM myself ( perldoc UNIVERSAL ). Thanks ( and +1 ) to all folks that pointed out my fault.

like image 1
Marco De Lellis Avatar answered Nov 07 '22 19:11

Marco De Lellis