Just a follow-up on this. It seems like there is an issue when inheriting from ‘Base_Item'. When I changed the inheritance to ‘Generic_Item’, and created a new method, it seems to be working fine.

Hope to have this tested over the next few weeks and then added to MH at some point.

On Nov 26, 2013, at 9:02 PM, H Plato <hplato@gmail.com> wrote:

Hi Kevin,

  I gave your suggestion a try, I created a ‘register’ method in the master object, and then added the child object as a new package in the same pm file:

sub register {
   my ($class, $object, $unit_num ) = @_;
   &::print_log("Registering Child Object $object->{unit_number} on unit $unit_num"); 
   $_[0]->{unit_object}{$unit_num} = $object;
   }

package mytest_Item;

@mytest_Item::ISA = ('Base_Item');

sub initialize
{
   my ($self,$object,$unit) = @_;

   @{$$self{states}} = (‘A’,'B’,'C');
   $$self{unit_number} = $unit;
   $$self{master_object} = $object;
   $object->register($self,$unit);

}

  11/26/13 08:53:52 PM: Can't call method "register" on an undefined value at ../lib/hptesting.pm line 1069.
 at ./mh line 31.
main::__ANON__('Can\'t call method "register" on an undefined value at ../lib...') called at ../lib/hptesting.pm line 1069
mytest_Item::initialize(‘mytest_Item=HASH(0x452eb70)') called at ../lib/Base_Item.pm line 39
Base_Item::new(‘mytest_Item', ‘master=HASH(0x452b440)', 2) called at (eval 380) line 211


And if I take out the $object->register line in the initialize sub, I get a weird errors;

  11/26/13 08:55:06 PM: Can't call method "isa" without a package or object reference at ../lib/Base_Item.pm line 59.
 at ./mh line 31.
main::__ANON__('Can\'t call method "isa" without a package or object referenc...') called at ../lib/Base_Item.pm line 59
Base_Item::add(‘mytest_Item=HASH(0x36b1988)', ‘master=HASH(0x36ae238)', 2) called at ../lib/Base_Item.pm line 40
Base_Item::new(‘mytest_Item', ‘master=HASH(0x36ae238)', 2) called at (eval 380) line 211

I must be missing something obvious...

On Nov 25, 2013, at 1:47 PM, Kevin Robert Keegan <kevin@krkeegan.com> wrote:

If you can, you probably want the state of the Master object to be set to the recently changed unit_number, or at least to some state to note that a new unit_number has been received.  If you do this, you can use the "tie_event" routine to trigger your set routine to run.  It probably makes sense for your master item and slave item objects to be different classes.

For example, something like this would work:

package Master_Item;

.......

sub master_event{
my ($self, $p_state) = @_;
if ($p_state eq 'SOME TEST'){
$$self{slave}->set($p_state);
}
}

package Slave_Item;

sub new {
my ($class,$master) = @_;
bless $self, $class;
$$self{master} = $master;
$$self{master}{slave} = $self;
$$self{master} -> tie_event ('$object->master_event("$state")');
return $self;
}

------

Anytime the state of the master object changes, master_event is run.  You would obviously need to expand this so it could work with many slave items, but that should be trivial.


On Mon, Nov 25, 2013 at 12:09 PM, H Plato <hplato@gmail.com> wrote:
I'm probably missing something obvious, but it's been a while since I played with creating an object.
 
I want to create a 'linked' type of object, that is one that will respond when a master object has changed. This master object listens to serial data, and will set it's internal structure based on what it sees. so when

$unit= $object->unit_now method equals a value, that means that an activity has taken place with this particular unit.
$object->unit_state($unit) method will give the state of that particular code

So using examples, I can craft up an object package, however I'm unclear how to link in the set method such that it fires when $object->unit_now equals the unit number stored in the object. My rough outline is below, I appreciate any feedback or suggestions object creators may have:

package test_item;

@test_item::ISA = ('Base_Item');

sub initialize
{
   my ($self,$object,$unit) = @_;

   $$self{unit_number} = $unit;
   $$self{master_object} = $object; #should be a reference to the original object, don't know if this syntax is correct
}

sub set
{
   my ($self,$p_state,$p_setby) = @_;

    if (unit_now $self->{master_object} eq $self->{unit_number}) {
       if ($self->{master_object}->unit_state($self->unit_number) =~ /^series1/) {
            $p_state = 'on';
         } elsif ($self->{master_object}->unit_state($self->unit_number) =~ /^series2/) {
            $p_state = 'off';
         }
 }

    $self->SUPER::set($p_state,$p_setby);
}


------------------------------------------------------------------------------
Shape the Mobile Experience: Free Subscription
Software experts and developers: Be at the forefront of tech innovation.
Intel(R) Software Adrenaline delivers strategic insight and game-changing
conversations that shape the rapidly evolving mobile landscape. Sign up now.
http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
________________________________________________________
To unsubscribe from this list, go to: http://sourceforge.net/mail/?group_id=1365