Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Creating Applications Using OO Techniques.

by Revelation (Deacon)
on Oct 10, 2002 at 02:00 UTC ( [id://204080]=perlmeditation: print w/replies, xml ) Need Help??

Object Orientedness In General.

Stereotypically the bless function is used to bless objects with a class name. My understanding is that the bless function is there specifically to allow for ‘Object Oriented Programming.’ Object Oriented Programming is defined as ‘object-oriented programming (OOP) is organized around "objects" rather than "actions," data rather than logic.’ (whatis.com) This lends itself to the taxonomical data (and code) structure that a lot of programmers use today.

However, the question of what constitutes an object seems to be hazy to me. The bless function, as well as many other object oriented structures can be applied to a more liberal definition of objects. CGI::Application illuminates this point well. There is not inherent data structure the object represents; instead it passes whatever the programmer deems to be consequential. However, it serves as a good way to ‘hide’ the internals of the program from the user (What could be easier than $app->run?), as well as model the generic process for CGI applications.

Can a process constitute data? What does constitute data? Are we allowed, or should we use OO functions, in a case like CGI::Application, if this is not true?

A Tangent Line:
To me, CGI::Application seems to be a perfect example of perl’s flexibility in objects. Disregarding the discussion about object oriented programming itself: Is the CGI::Application package just an attempt to do what is ‘intuitive’, and therefore just utilizing an inherent ‘feature’ of perl? Can a person misuse that feature?
Gyan Kapur
gyan.kapur@rhhllp.com
  • Comment on Creating Applications Using OO Techniques.

Replies are listed 'Best First'.
Re: Creating Applications Using OO Techniques.
by dws (Chancellor) on Oct 10, 2002 at 02:23 UTC
    However, the question of what constitutes an object seems to be hazy to me.

    At a high level, an object is a collection of behaviors and data. At the edges, some objects are strictly behavior, while others are little more than data. Some objects mimic things we know from the real world, while other objects capture abstract notions that have no direct counterpart in the physical realm. Some objects are abstract templates intended for refinement by "concrete" subclasses.

    CGI::Application is an interesting case. It is sort of a template function that encapsulates a switch on data provided from URI arguments. Is it a shining examplare of object modeling? Debatable. Should you be "allowed" to use it. Sure.

    If you really want to understand objects at a deep level, I recommend that you spend some time playing with Smalltalk. In Smalltalk, everything is an object, including some abstract things like Classes and Processes. Squeak Smalltalk is freely downloadable.

•Re: Creating Applications Using OO Techniques.
by merlyn (Sage) on Oct 10, 2002 at 02:10 UTC
    An "actor" or an "agent" is certainly an object. But if the "actor" is merely coordinating or managing a single other category of objects, that behavior is best taught to that object instead.

    -- Randal L. Schwartz, Perl hacker

Re: Creating Applications Using OO Techniques.
by jepri (Parson) on Oct 10, 2002 at 07:11 UTC
    A program can constitute data (if that's what you mean by process). Certain programming languages treat the code as data, and a program can process it's own instructions.

    Perl keeps a distinction between code and data. To make an object, you take some data, like a hash, and 'bless it'. This tells perl that if it sees a -> arrow, it should check to see if there is a subroutine for that object.

    You can work around Perl's separation of data and code using the 'eval' command, which allows you to take data and run it as code.

    You can also almost treat code as data. By saying that $code = sub { print "This is really code\n";} you can create a variable that 'holds' code. You can pass it to functions, put it in a hash, but you can't modify it.

    CGI::Application could use any of the above methods to do it's code and data magic. You are certainly allowed to do it any way you like, but each way has it's limits for tricks you can do further down the line. Basically if it works for you, and it feels good, you should do it.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Creating Applications Using OO Techniques.
by mstone (Deacon) on Oct 11, 2002 at 02:56 UTC

    An object is a Random-Access Machine.

    The Random-Access Machine (RAM) is a theoretical model for the digital computer. It has a bank of addressed memory where it stores information, and a set of built-in commands which modify that information. The term 'random-access' means that you can read or write information to the memory addresses in any order, rather than having to step through them sequentially. Sequential-access machines would be those that step through the data one item at a time, like a queue, or moving forward and backward, like a stack.

    (As an aside, the queue-based sequential-access machine is called a state machine. The stack-based sequential-access machine is called a pushdown automaton. A function that only stores information in one variable is equivalent to a state machine, and a set of nested functions, with input parameters and return values, is equivalent to a pushdown automaton. A set of functions that share two or more global variables are once again equivalent to a RAM)

    An object is a RAM because it has a bank of addressed memory (its attributes) and a set of built-in operations that modify its information (its methods). The fact that an object's memory addresses tend to be human-readable names rather than numbers is irrelevant. So is the fact that different addresses (attributes) can store different data types. The whole 'numerically-addressed 8**N-bit registers' thing is just a convention that happened to be easy to burn into silicon.

    An OO application is a network of Random-Access Machines.

    The typical OO application consists of a set of objects that communicate by triggering each other's methods. Those objects share data in various ways, which are known as types of linking. The three most common types of linking are:

    • Direct linking, also known as inheritance,
    • Data linking, also known as shared memory,
    • and Function linking, also known as input paramaters and return values.

    Inheritance effectively embeds one RAM inside another. All the embedded RAM's memory and operations are accessible to the embedding RAM. The embedding RAM 'inherits' all the embedded RAM's storage and abilities, and can add further storage or abilities as the programmer sees fit. Inheritance creates the greatest amount of interdependence between the embedding RAM and the embedded one, therefore it is said to have the tightest coupling of the three types of linking.

    Data linking makes one or more chunks of addressed memory visible to two or more RAMs. For unrestrained data linking, all the RAMs that share the memory can read or write data at any time. This can generate all sorts of problems with two or more RAMs trying to modify the same address at the same time, which are generally known as concurrency issues. You can control concurrency problems with locking protocols, but those get almost as hairy as concurrency problems per se. About the only safe way to handle data linking is to give one RAM the power to write to the address, and restrict all the other RAMs to read-only access. Because of these problems, data linking has the second-tightest coupling of the three linking methods.

    Function linking has the loosest coupling of the three linking methods, because input parameters and return values impose automatic read-only constraints on the values being passed. Even within that realm, though, there are tighter and looser forms of coupling. Call-by-value likning is the loosest and most bulletproof form of linking currently available, because nothing actually travels between the caller and the callee. The application's runtime environment reads the value indicated by the caller, and writes a copy to the memory location of the callee's input parameter. Nothing the callee does can affect the caller's stored data in any way. Call-by-name linking (i.e.: passing a pointer) has tighter coupling than call-by-value, because once you dereference the pointer, you have to deal with all the problems of data linking again.

    What all this has to do with the original question:

    If you want to understand OO techniques, you have to know how objects work. Go back and take another look at CGI::Application, and look at the relationships between data storage and data manipulation. You can't grok the OO mindset if you only focus on the data or the manipulations. Look at who's responsible for what chunk of data, and how the various pieces communicate with each other. That should give you a better idea of what objects are, and why people choose to put feature X in class Y.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-25 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found