Here's a small method:
sub sort_by_value { my $set = shift; print "start\n"; print join(',',map {$_->value} @{$set->{"cards"}}); print "end\n"; @{$set->{"cards"}} = sort {$a->value <=> $b->value} @{$set->{"cards" +}}; }
in case you are wondering, $set->{"cards"} contains an array of cards objects that each have a value method, that returns the value of the card. So far so good. You'll notice the print statement calls the value() method for each card, just before the sort block does the same thing. So:
  1. I get a 'can't call method value on an undefined value' error, referring to the sort line.
  2. I get this even though the print line two above works fine on the same call to this method.
  3. If I run perl with the -d flag, I don't get this error.
  4. The error generally appears between the 2nd and 7th time this method is called during program execution.
  5. This method is itself called from a grandparent method that in turn is invoked in a sort {} block that ranks the values of whole hands of cards. I can only replicate the error when using a sort block to call the grandparent method. Other invocations are fine.
  6. Not only that, but if I make just one invocation of the grandparent method (even in a null context) on just one of the objects being sorted before the sort routine is started, then the bug vanishes - all things are sorted OK.
I'm completely baffled.

In reply to Vanishing bug by steer

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.