Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I created a Moose object which has a method which produces a DBIx::Simple instance. When this method is called and the results placed in a scalar:
my $s = constructor(); my $dbs = $s->dbs; my $r = $dbs->query($Q); my $h = $r->hashes;
then everything works fine. But, but when you attempt to chain together the method calls:
my $s = constructor(); my $r = $s->dbs->query($Q);
the result is not the same - you get a dead object.

The reason this happens is that on line 165 of Simple.pm there is an attempt to reduce the reference count by double-quoting the database handle related to the statement handle:

# $self is quoted on purpose, to pass along the stringified version, # and avoid increasing reference count. $st = bless { db => "$self", sth => $sth, query => $query }, 'DBIx::Simple::Statement'; $statements{$self}{$st} = $st;
A fix that works for me is to simply remove the double quotes. Then all 3 test cases in the test program below work. So I guess my questions are:
  1. The simple fix for me is to pass the actual database object instead of the stringified version. is there any practical case where passing the actual database object would lead to problems?
  2. What is it about method chaining versus several separate method calls that is leading to premature object death?
  3. Is it reasonable to expect method chaining to work the same as breaking down the method chain into a series of separate calls?
  4. If there are cases where having the database object as part of the statement object poses problems, then how can we support method chaining and also avoid the problems caused by it?

personally, I don't understand why line 165 of Simple.pm looks the way it does. I asked Juerd about it and here is what he said:

> Also, is there a reason you didnt use Scalar::Util::Weaken instead o +f > quoting an object like that? I'm not entirely sure what the reason was in this specific case, but i +n the general case I think it's safer to just have a string, than to hav +e something that when copied does increase the refcount, and could still be destroyed when you didn't expect it yet. If all you need is somethi +ng to recognise the object when you see it, then keeping the entire objec +t around is just unnecessary bloat; bloat that could cause many hours of debugging.
And that makes no sense to me:
  • Increasing the refcount is no big deal. It just means the statement handle is not extinguished until the database handle is
  • There is no bloat in adding the reference because it is not cloned. It's the same reference, just referred to in one more place.


In reply to method chaining fails where separate method calls succeed in DBIx::Simple 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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (17)
    As of 2014-07-11 14:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (227 votes), past polls