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

Monks, please bear with me here, my OO vocabulary is not always accurate :P
I am concerned about one of the ways I have used to pass an object between classes, using bless.
$job is an object reference to a hash in the package oo::job
and $job is an object I am passing to the constructor oo::gui::defaultjob->new
once in the constructor,
package oo::gui::defaultjob; use strict; sub new { my $self = shift; my $job = shift; my $oldclass = ref $job; bless $job, $self; # Call methods of defaultjob on $job, since they depend on # the hash as setup by oo::job MainLoop; # Invoke Tk, allowing user to gui'ly modify the job return bless $job , $oldclass }
Thus $job is returned as belonging to the oo::job package.
My question : What nastiness have I introduced here ? or is this an over-complication of an easier way.
Reading the Damian Conway's OOPerl book, which is making the cracks which flood the brain with light, but I am certainly still learning.
Constructive Abuse only pls!

Replies are listed 'Best First'.
Re: Revolving door blessing.
by chromatic (Archbishop) on Nov 06, 2002 at 07:03 UTC

    Yes, this is overcomplicated. It sounds as if you have objects that are too broad. Why not just pass the hash as an argument to the methods? Otherwise, you could let oo:job inherit from oo::gui::defaultjob and override as necessary.

Re: Revolving door blessing.
by robartes (Priest) on Nov 06, 2002 at 07:53 UTC
    I agree with chromatic - you're overcomplicating things. You're 'temporarily' blessing a hash in class oo::gui::defaultjob that was already blessed in $oldclass. You then perform some oo::gui::defaultjob magic on it and rebless it into the old class.

    This is a case for inheritance: have oo::job inherit from oo::gui::defaultjob (as chromatic suggests) and then you can just do the magic in oo::job. The methods that are not found in oo::job will be searched for in oo::gui::defaultjob.

    And as to nastiness, just consider what you're doing above: your constructor of class oo::gui::defaultjob returns an instance of class oo::job...

    CU
    Robartes-

Re: Revolving door blessing.
by submersible_toaster (Chaplain) on Nov 06, 2002 at 08:56 UTC
    ++chromatic , ++robartes
    Aha - I think I get it. Having not used inheritance yet I failed to recognise the case for using it. So if I have this right.....
    package oo::job; @ISA = qw/ oo::gui::defaultjob/; use strict; sub new { }; # Make my hash and bless into oo::job 1; package oo::gui::defaultjob; @ISA= qr/ Tk /; sub build_gui { }; #packing widgets into a toplevel object sub display{} ; #invoke MainLoop on toplevel 1; package main; use oo::job; my $job = oo::job->new( %options ); $job->build_gui(); $job->display;

    Now if that is even close , then my program just got about 1000 times easier to build.

    submersible_toaster shuffles off to the library for further wisdom
      That's it. Perl will look for build_gui() and display() in oo::job, and, failing to find it, will search in its superclass, in this case oo::gui::defaultjob. BTW, if it didn't find these methods in oo::gui::defaultjob, it would travel further up the tree to Tk, so you can even call Tk methods on $job.

      CU
      Robartes-