Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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 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 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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2018-06-21 22:21 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (120 votes). Check out past polls.