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!


In reply to Re: Missing object reference in sub by naikonta
in thread Missing object reference in sub by Anonymous Monk

Title:
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, details, 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, summary, 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.