--- Anish Kumar K <[EMAIL PROTECTED]> wrote: > Hi > In java we have instance operator to tell the type of Object. > > Similarly in perl say I am passing a reference object to a function, > > Is it possible to detect in the function whether the object is HASH > or ARRAY...
If you need to know the internal structure of an object, there's a design error somewhere. One of the strongest benefits of OO programming (some would argue the only benefit :) is implementation encapsulation. Objects are merely responsible agents. Tell 'em what to do and let 'em do it. If you have to peek inside or know the internal structure, you're violating encapsulation and it means that the object isn't doing it's job (or you're not. Who knows?) If you do find yourself peeking at the internals, the author no longer has the freedom to change the internals to better represent the object's needs lest he or she break the code which relies on the object. Even if you're the author, sooner or later you'll be bit if you rely on this. One example: my Array::AsHash module relied on Class::Std and, as a result, no one could peek inside. However, I needed to make a significant change to how this module worked and converting to a blessed hashref made life *much* easier. As a result of my implementation encapsulation, I completely changed the internals, leaving only the external API the same. Not a single person has complained, even though I know a few folks use this module. Here's a second example, one which makes me want to scream, even though it *looks* reasonable: sub foo { my $self = shift; my $dbh = $self->{dbh}; ... } The author of this code claimed that this was OK because it's an object only peeking at itself and it was "designed to be used that way". The problem is that this was a subclass two inheritance levels away from the class setting the $dbh! As a result, when I needed to have better control over what gets set as the $dbh (some subclasses needed restricted permissions), I couldn't do that without breaking a superclass. What I really needed was this: sub foo { my $self = shift; my $dbh = $self->_dbh; ... } That would allow me to override the _dbh() method in a subclass and have the control I needed. Don't peek inside objects! See also "Thou Shall Not Covet thy Object's Internals" for another horror story. http://www.perlmonks.org/?node_id=75578 Cheers, Ovid -- Buy the book -- http://www.oreilly.com/catalog/perlhks/ Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/