in reply to Missing object reference in sub

According to the docs (perlobj), there are three basic rules in Perl OO:
  • An object is simply a reference that happens to know which class it belongs to.
  • A class is simply a package that happens to provide methods to deal with object references.
  • A method is simply a subroutine that expects an object reference (or a package name, for class methods) as the first argument.
The rule number 3 dictates that a method can be an object method when it expects an object reference as the first argument (called as object method), and can be a class method when it expects a class or package name as its first argument (called as class method). There is, however, a technique to make the method handles them both, depending on how it was called. So, to make your methods take the first argument as object reference, you have to pass it in the object. The way it's done in Perl is using the arrow operator.
my $obj = Class->new; $obj->a_method($param1, $param2..., $paramN);
The $obj before the arrow automatically becomes the first argument for a_method. I don't know how the sum() method is called from Frontier::Daemon. But you specify the sum parameter to have value of CODE reference to the sum() method. Also, I don't know in which context the method is defined. If it's merely a callback, you don't have to make an object method.

But let's assume that sum() is defined in a class called My::Daemon. Then you need to provide the callback in certain way so the sum() method is called with the My::Daemon object.

package My::Daemon; use strict; use warnings; use Frontier::Daemon; sub new { my($class, @args) = @_; .... my $self = {}; # empty anonymous HASH ref bless $self, $class; # make it an object and return it } sub some_method { my $self = shift; my $id = Frontier::Daemon->new( methods => { sum => sub { # anonymous sub as callback my @args = @_; # @args received from F::D somewhere .... $self->sum(@args); # call sum as object method }, }, ... other params ); } sub sum { my($self, $arg1, $arg2) = @_; ... } 1;
Now, the $self variable contains the object reference of My::Daemon class.The $arg1 and $arg2 are from @args passed in from the anonymous subroutine.

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!