Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

has-a in Perl

by skazat (Chaplain)
on Apr 18, 2005 at 19:45 UTC ( [id://448994]=perlquestion: print w/replies, xml ) Need Help??

skazat has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I'm trying to figure out a good way to do this in Perl.

Let's say I have an OO module, Widget and I have another, Widget::CheckBox. Widget::CheckBox is-a Widget - it inherits many of the things from Widget via the @ISA() array. How do I work with Widget::CheckBox from inside the Widget Object?

Do I just make a new instance of Widget::Checkbox within Widget? That sounds sort of wasteful. It seems I should be a ble to pass a reference to my Widget self, directly to Widget::Checkbox and have Widget::Checkbox see if it gets passed something like that. Basically, it would seem I want to do *both* of these things:

use Widget; my $w = new Widget; return $w->widgets->checkbox; # --- or -- use Widget::Checkbox; my $wc = Widget::Checkbox; print $wc->checkbox;

Am I making sense? :)

I'm guess I'm not clear on working with Perl while using a, "Has A" relationship;

 

-justin simoni
skazat me

Replies are listed 'Best First'.
Re: has-a in Perl
by Roy Johnson (Monsignor) on Apr 18, 2005 at 20:00 UTC
    The 'A "has-a" B' relationship is implemented by creating a member of class B within an object of class A. They are generally disparate types.

    What you are describing sounds like you want a parent class to be aware of a derived class, and that doesn't sound like a Good Thing to me. What are you trying to accomplish, in slightly more concrete terms?


    Caution: Contents may have been coded under pressure.

      I'm basically just trying to make a method of my base class in a separate module (file) - but the method needs methods of the base class to work correctly.

      It would be cool to be able to call this new method in my program via the base class or the separate module; So if I want to work with just Checkboxes, I got it, if I want to work with all my widgets, got that too - not a good idea?

       

      -justin simoni
      skazat me

        It would be cool to be able to call this new method in my program via the base class

        That's what subclassing does: you can call any method from the base-class, and if the subclass implements or overrides that method, the subclass's method will be called.

        package Base; sub method1 { my $self = shift; $self->method2(); } sub method2 { print "Base\n"; } package Sub; @ISA = qw(Base); sub method2 { print "Sub\n"; } package main; Base->method1; Sub->method1; __END__ Base Sub
Re: has-a in Perl
by cowboy (Friar) on Apr 18, 2005 at 20:17 UTC
    Beware circular references. They are very easy to create if you start passing references to the current object you are in, into other sub-objects, which may hang onto them.
    Your current plan also sounds more like the objects are inter-dependant, rather than one inheriting from the other.

    Widget and Widget::Checkbox are not separate objects, merely one object spread over 2 packages. I would suggest separating them, keep Widget as a base class, which knows nothing about Widget::Checkbox. Let Widget::Checkbox (and Widget::Radio and Widget::Foo extend Widget)

    In one of my current projects, I use a base object, which contains these methods:
    # initialize/bless/ect $obj->new(); # serialize the objects data (currently with Storable) $obj->serialize(); # get/set params as in CGI.pm $obj->param(); # deserialize the objects data (Storable) $obj->deserialize(); # xml format $obj->to_xml(); # de-xml $obj->from_xml();
    All of my other data objects, inherit from this object. A few, override the object's methods such as param, if I need a specific behaviour. (such as throwing an exception if I get bad data passed in)

      If that were me, I'd use roles instead of inheritance. (Class::Roles).

        Thanks, I'll have a look at them.
Re: has-a in Perl
by dragonchild (Archbishop) on Apr 18, 2005 at 19:52 UTC
    I'm not sure I fully understand your question. In the example you're giving, Widget is an abstract base class. You shouldn't ever be working within the Widget class.

    As for shared code, your code in Widget will look something like:

    sub some_method { my $self = shift; # Do stuff with $self. # At this point, $self will be a Widget::CheckBox }

    If that doesn't help, please reply to my post with clarification. (Do not change your base post!)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found