Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: perspective and object-disorientation

by MarkM (Curate)
on Jan 17, 2003 at 23:29 UTC ( [id://227862]=note: print w/replies, xml ) Need Help??


in reply to perspective and object-disorientation

Ctrl-z writes:

So, theorhetically, is manipulating an objects inheritence based on how it is currently being percieved, a worthwhile cause for meditation?

Without intending to, you have pointed out the exact reason why so many people new to Perl have difficulty when understanding the effects of modifying @ISA.

You say "manipulating an object's inheritance". The only way to manipulate an object's inheritance is to bless() it into another class:

my $object = Class1->new; bless $object, 'Class2';

Manipulating @ISA does not change the inheritance of an object. It changes the implementation of the class. The effect of this is that all objects that are blessed into the class suddenly inherit a different set of methods. To this end, we find chaos. Perl attempts to improve the performance of method invocations by caching the results of method lookups. These cached results become incorrect when @ISA is alterred. Also, from a code re-entrancy requirement, since changes to @ISA are global, it would be very difficult for two sections of code that had different 'perspectives' to access the same set of objects at the same time.

You have a good point in your node -- the perspective of the programmer, and the requirements for the project dictate the class names that are decided upon. These names may be inaccurate to a programmer with a different perspective, or a project with a different set of requirements.

The most common solution to this problem is the class hierarchy. The Employee class that you refer to above would perhaps better be named Acme::Payroll::Employee. This additional context specifies the context under which the class should be perceived from.

Ctrl-z writes:

Of course, an Employee may also be a Musician, Artist, Trainspotter (in their spare time), or a Father, Husband, Buddy, or a Jerk, Hero, Nondescript-Bystander. There is no little tree diagram that can display this, because largely it is a matter of perspective. Once you have the perspective of the "viewer" (perhaps the user, or another object) then the tree of inheritence can organise itself. Its not the One True Tree of the Employee class - its just a perspective.

In English, we say 'Mark is an artist.' or 'John is a musician.' From a design perspective, however, 'Artist' is a professional role. The question "Is there an artist in this room?" is programmatically similar to:

my @people_in_room = grep {$_->isa('Person')} $room->contained_objects +; my @artists_in_room; for my $person (@people_in_room) { push(@artists_in_room, $person) if grep {$_->isa('Artist')} $person->professional_roles; }

The relationship between real life objects is quite sophisticated, and is often not a case of inheritance. In the programming domain, object models rarely ever need to be as complicated as they are in real life domain. Simple problems call for simple solutions.

UPDATE: Abigail-II is insistent that the method cache problem is not an issue, since it is fixed in recent versions of Perl. Although he could have been more polite about it, I accept his criticism, and have striken out the offending sentence. Cheers!

Replies are listed 'Best First'.
Re: perspective and object-disorientation
by Abigail-II (Bishop) on Jan 18, 2003 at 00:30 UTC
    Perl attempts to improve the performance of method invocations by caching the results of method lookups. These cached results become incorrect when @ISA is alterred

    Oh, come on. Please get a newer version of Perl. I reported this bug in the beginning of 1997, and it was fixed shortly after. The Camel-II explicitely mentions that modifying @ISA clears the cache.

    You're claiming that modifying @ISA is bad, because it changes the implementation of the class. But is that always bad? Installing a sub in an AUTOLOAD also changes the implementation of a class, and that's something done often.

    I wouldn't recommend modifying @ISA, unless you're really sure what you are doing. But I also won't claim modifying @ISA should never been done.

    Abigail

      I wouldn't recommend modifying @ISA, unless you're really sure what you are doing. But I also won't claim modifying @ISA should never been done.

      See Test::Class & self-shunt pattern for one example where altering @ISA might be useful :-)

      Abigail: Come 'on' yourself. You must be having a crappy day to take only one thing out of my node and s$it on it. :-)

      I never claimed that modifying @ISA should never be done. Just, those who normally resort to modifying @ISA, are new to Perl, and don't really understand what they are doing.

      And even for people that do know what they are doing... it doesn't hurt to take a second look and consider whether there might not be an alternative that is more portable to other languages.

        If I wanted to write code that's more portable to other languages, OO would be one of the last things I'd consider using. Just one instance variable per object! I don't know any other language developer who considered doing that!

        Now, I wouldn't want to write code that's portable to other languages, but that's something else.

        Abigail

Re^2: perspective and object-disorientation
by adrianh (Chancellor) on Jan 18, 2003 at 19:04 UTC
    Also, from a code re-entrancy requirement, since changes to @ISA are global, it would be very difficult for two sections of code that had different 'perspectives' to access the same set of objects at the same time.

    While I don't think changing @ISA willy-nilly is a good idea, changes are global only if you choose to make them that way.

    @ISA can be changed locally using... well... local :-) It can be a useful idiom when testing to make one object temporarily appear as another.

      Changing @ISA can be a useful idiom when testing to make one object temporarily appear as another
      But only if you insist on trying to turn Perl into a pessimistically typed language. Us optimistic types just implement the appropriate methods and have done with it. But we've done this discussion already haven't we :)
Re: Re: perspective and object-disorientation
by Aighearach (Initiate) on Mar 28, 2003 at 07:24 UTC
    UPDATE: Abigail-II is insistent that the method cache problem is not an issue, since it is fixed in recent versions of Perl. Although he could have been more polite about it, I accept his criticism, and have striken out the offending sentence.

    I propose that both correctness, and Perl, are much more interesting than politeness. When I was first getting started, Abigail was probably the most useful person to in my learning. Precicely because he would skip all the niceties and just tell me, "no that's totally wrong, ..."


    --
    Snazzy tagline here

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://227862]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found