Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to morph a plain module to OO

by Revelation (Deacon)
on Sep 07, 2002 at 20:53 UTC ( [id://195903]=note: print w/replies, xml ) Need Help??


in reply to How to morph a plain module to OO

One thing that is quintisential to all object orriented programming is inheritance. I don't believe your script allows for that (what if module script use()es it, and put @ISA = yourModule). I'm sure there are other ways to preserve inheritance, but the one I've used is that of CGI.pm, which has a self_or_default function. That function creates a blessed object, if there is not one to begin with. A more simple version, which would be fine for you would be:
use strict; my $Q; sub return_with_object { unless (defined($_[0]) && (ref($_[0]) eq 'ModuleNAME' || UNIVERSAL::isa($_[0],'ModuleNAME' +)) ) { $Q = ModuleNAME->new unless $Q; unshift(@_,$Q); } return wantarray ? @_ : $Q; }
All the subroutine does is checks that an object is defined, and that it is the object for the class you are using. If there is no object, the script creates one, for use throughout the script, without the user knowing that it exists. It's actually a really cool idea, because an oblivious user can't mess with an object, although it exists, but I'm sure some guru has a reason not to use it. It also preserves inheritance by using Universal::isa (UNIVERSAL::isa ( VAL, TYPE ) ), which returns true if the VAL is the name of a package that inherits from (or is itself) package TYPE.

Update: And to use the subroutine -
sub SomeSubWithNothingElseToShift { my $self = return_with_object(@_); } sub SomeSubWithOtherStufftoShift { @_ = return_with_object(@_); my $self = shift; my $barz = shift; }

Gyan Kapur
gyan.kapur@rhhllp.com

Replies are listed 'Best First'.
Re: Re: How to morph a plain module to OO
by rir (Vicar) on Sep 08, 2002 at 18:18 UTC
    By definition you can not have more than one quintessence of
    something(1). I do agree that inheritance is the
    fundamental element or the typical characteristic of OO.

    (1) Apologies to the Humpty Dumpty-ists among us.

    That does not mean that every class should be inheritable.
    A mechanism to prevent the inheritance of classes is a
    good thing. That inheritance was not anticipated during
    the design of a class, or a the usage of a class being
    deprecated are two reasons off the cuff.

    I don't know of such a mechanism for Perl. In Perl
    this might be expensive.

    To the initial query:
    Start another package to avoid disturbing the your existing
    clients.

    I appreciate the return_with_object idea, but
    see it as necessary in certain social situations. Coding
    so as to protect oblivious coders seems like a silent
    endorsement of oblivion. I realize this is not necessarially
    so, nor always a bad thing; but to write to a level
    below your abilities cannot be good in itself.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-18 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found