I'm confused. At one point you say the constructor never returns, but the example shows using it in assignment and in code afterward. For my possibly helpful reply, I'm assuming that it does return.

package Frontier::NotReally; sub new { my $class = shift; my %p = @_; return bless \%p, $class; } sub id { my $self = shift; return "$self"; } sub call { my $self = shift; return $self->{sub}->( @_ ); } package main; my $id; $id = Frontier::NotReally->new( sub => sub { print "insub: ", $id->id( +), "\n" } ); print "outsub: ", $id->id(), "\n"; $id->call( 'sub' ); __END__ outsub: Frontier::NotReally=HASH(0x6f1940) insub: Frontier::NotReally=HASH(0x6f1940)

I don't know how much my mockup, Frontier::NotReally, matches the actual Frontier::Daemon you're dealing with, but it's close enough by my understanding. The important part of this example is that my lexical declaration of $id comes before its actual assignment.

my $id; # now $id exists! $id = F::D->new( foo => sub { 'here, $id exists too' } );
What happens is that at the time you call the constructor, $id is undef. Once the constructor finishes, $id has the object's reference in it, and the anonymous sub you passed into the constructor can use it. This won't work if the constructor itself is calling your callback, but any time after that, you'll have the object ready to work with.

Update: I see chromatic has explained faster than I have.


In reply to Re^3: Missing object reference in sub by kyle
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.