I've run into some strange behavior when passing objects around in Perl 5.6.1 I'm writing a class ("Emitter") that represents a hidden state in a hidden markov model. An Emitter instance contains a transition table, which contains other Emitter instances. The idea is to have a transition method so that calling $emitter->transition() returns the next Emitter in the Markov chain. The problem is that running :
$next_emitter = $current_emitter->transition(); print $next_emitter->emit() . "\n";
results in
Can't locate object method "emit" via package "Emitter=HASH(0x1016c9c0)" (perhaps you forgot to load "Emitter=HASH(0x1016c9c0)"?) at HMM/ODCRollGenerator.pm line 100.
Indeed,
ref($an_emitter->transition());
results in Emitter=HASH(0x1016c9c0) whereas
ref($an_emitter)
gives Emitter as expected.

Within an Emitter, the transition table is stored as a hash, with the other Emitter instances as the keys, and the transition probabilities as the values. i.e. the setter method looks like this:

sub transitions { my $self = shift; if (@_) { $self->{TRANSITIONS} = shift; &_build_prob_table($self); } return $self->{TRANSITIONS}; }
and it might be called like this:
$self->{START} = Emitter->new(); $self->{FAIR} = Emitter->new(); $self->{LOADED} = Emitter->new(); ... $self->{START}->transitions( { $self->{FAIR} => 0.5, $self->{LOADED} => 0.5 } );

Now, I can make things work by re-blessing the reference returned by transition(), but this seems awfully kludgy, and I suspect there is a better way to do this. Besides, as I understand it, it's the underlying thingy, not the reference, that is blessed. Anyone know what's going on here? Is there some magic I need to add to the class to keep the blessing attached to objects?


In reply to objects becoming unblessed? by omnibus

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.