I'm positive many folks will read this post and say, "Well, obviously." But nonetheless the idea was new to me, so I am tossing it out....

My former concept of objects was that they make sense My minor epiphany is the power of using objects even for The example at hand is getting data from a file about some complex thing. During the course of the project, I've repeatedly changed the file structure, adding and dropping fields. And the next step will be to skip the file and just move the data into a real database. Whatever. By putting a thin object wrapper around the whole concept (my $value = $C->get(var=>$var, subid=$sid);) I can make these changes without any impact on the large main program. Cool.

Again, this is probably old news for many monks, but it got me thinking about objects in a new way.

Replies are listed 'Best First'.
RE: Power of single objects
by Fastolfe (Vicar) on Oct 19, 2000 at 18:55 UTC
    Personally, I feel that some modules nowadays are trying to use objects because it's "cool", even though the module would be better off being used in a functional manner.

    If a module doesn't need to keep track of state information at all between "methods"/functions, you generally don't want to use an object-oriented approach. If it's possible/likely that the user (the developer) will want to create multiple objects/sets of state information and manipulate each set individually, an object-oriented approach is pretty much a must.

    For stuff in between, though, don't underestimate the value of passing arguments to simple functions, exported by your module. If you feel that your module will easist to use in a functional capacity, but you'd still like to offer the developer the ability to create objects, why not just write your module to support both methods (a la CGI.pm)?

    For example, DBI needs to use objects. Without them, we would be unnecessarily preventing the developer from working with two databases at the same time. Array::Compare, however, does not need to use objects (it does!). Why do we need to instantiate a "comparator" object to perform our array comparisons? A function, with perhaps optional arguments, would be a lot easier to use and read. Plus, it would allow us to use function prototypes if we felt the need (a plus in Array::Compare's case, for instance).

    So don't jump on the OO train just because it's a nifty way of doing things. If your code would benefit functionally from using objects, or you have a need to keep track of isolated state information, fine, but weigh your module's usability against power and try to look at both ends of the spectrum.

      Personally, I feel that some modules nowadays are trying to use objects because it's "cool", even though the module would be better off being used in a functional manner.

      My dear Fastolfe, thou are a bit slow: modules serve to encapsulate variables, even for a collection of related functions. 'tis not only for OOP that thou wilt find modules of great use in parting the red seas of programming jobs that lies ahead for ye.

        Hmm.. Maybe one of us is misunderstanding the other.. I have no problem with modules, I just have a problem with the overuse of OOP in cases where it just adds complexity without any benefit. Consider some module "Object", which has the sole task of performing some_operation on $item, the behavior of which could change, depending on the value of some_variable.
        use Object; my $obj = new Object; $obj->some_operation($item); $obj->some_variable($argument); $obj->some_operation($item); # versus use Object; some_operation($item); some_operation($item, $argument);
        In my opinion, there are many types of modules that, for whatever reason, unnecessarily go with an OO approach, even though the paradigm doesn't quite fit, and we get little benefit out if it. I really don't know how to describe the best way to decide. For me, I just look at my package in both ways. If it "feels" right in an object point of view, if I can see a benefit to creating multiple "instances" of my module in the form of objects and toss those around, I'll build it in OO style. If my resulting implementation is going to be a lot more efficient and simpler in a functional style, I'll go that route. Perhaps it's just personal preference...
RE: Power of single objects
by princepawn (Parson) on Oct 19, 2000 at 18:25 UTC
    The two ways to make manifest your intent to use singleton objects in Perl are:
  • The module Class::Singleton by the ubiquitous Andy Wardley
  • Making use of class methods and state instead of instantiating an object at all.
RE: Power of single objects
by AgentM (Curate) on Oct 19, 2000 at 21:37 UTC
    The reason is see power in C++ in contrast to Java is because C++ offers one to still be able to use functional code. When I'm writing a larger program, I may prefer to have a few variables or functions that i don't want in classes of any type. If I call them often or it just doesn't make sense to encapsulate various vars in a class, then I'm not forced to. Along the same lines, perl forces no one to use OO programming and Perl is even less OO (in my humble opinion) than C++. Look at any complete code and the integral parts of the program are most likely functionally-based which incorporate nifty CPAN modules. To give an example of my above comment on things that I may not wish to encapsulate is:
    • a single variable that really has nothing to do with any others (waste of time to throw in a class with various protections that I can keep track of myself
    • or a bunch of random variables that have nothign to do with each other and would be otherwise lonely variables (it wouldn't make sense to bundle them together)
    • functions I'll be using alot (skipping a class name may save me typing in the long run:-O)
    • global variables that I'll be using alot that are encapsulated in their own right (like my own personal %configuration hash).
    And that's what I love with Perl /\/\
    AgentM Systems nor Nasca Enterprises nor Bone::Easy is responsible for the comments made by AgentM.</h6>