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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Ah. I think I see what you're trying to do now. Let me re-state what I think it is, just in case I'm getting the wrong end of the stick.

  1. You have a family of widget objects that all have the same interface (new, initialise, run, default and add).
  2. new(), initialise(), and run() are common to all objects.
  3. What default() and add() do is dependent on the 'area' param of the CGI object.
  4. Whether run() calls default() or add() depends on the 'act' param of the CGI object.

I'm assuming that there is some reason that you have a separate initialise() method - perhaps you need to call it multiple times on the same object. If not, it might as well be called by new() at object creation time.

If the above outline is correct then some kind of state transition pattern would seem appropriate. There are several ways you could do this. Two that spring to mind are:

  1. You could store the different default/add subroutines in a hash $action based on the 'area' and 'act' parameters and make run() lookup the appropriate subroutine. This way you remove all the Widget* subclasses, which probably won't be what you want to do.
  2. You could dynamically change the state of the object to match the appropriate Widget class depending on the area parameter.

The latter method is very simple to implement. Indeed, if you change the parameter passed to 'area' to match the classname exactly (so it's "WidgetOne" rather than "widgetone" it becomes almost trivial.

sub run { my $self = shift; my $class = $self->{CGI}->param('area'); # We want to be careful that we're reblessing into a sensible cla +ss croak "$class not a subclass of OurParentPackage" unless UNIVERSAL::isa($class, 'OurParentPackage'); bless $self, $class; if ($self->{CGI}->param('act') eq 'add') { $self->add; } else { $self->default; } } package WidgetOne; use base qw(OurParentPackage); sub default { print "WidgetOne default\n" }; sub add { print "WidgetOne add\n" }; package WidgetTwo; use base qw(OurParentPackage); sub default { print "WidgetTwo default\n" }; sub add { print "WidgetTwo add\n" }; ... and so on for the other Widget* classes ...

The above solves the problem quite neatly, and you can add new Widget* subclasses without touching run().

I still don't know the purpose of the Widget classes, so this advice may be wrong, but I would also seriously consider refactoring the CGI object outside the Widget class hierarchy. It looks a little like an MVC pattern waiting to happen.

Hope this helps.

In reply to Re^4: Passing self from class to class? by adrianh
in thread Passing self from class to class? by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (3)
    As of 2017-11-25 03:05 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (355 votes). Check out past polls.