steer has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Vanishing bug
by Aristotle (Chancellor) on Jan 24, 2005 at 22:19 UTC

    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.

    Localize $a and $b at the beginning of your method.

    Makeshifts last the longest.

Re: Vanishing bug
by Tanktalus (Canon) on Jan 24, 2005 at 20:46 UTC

    It'd be really nice if you could put together a self-contained full runnable example of the problem. That said, these types of problems are often hard to boil down. Also, if you're using perl < 5.8, I'd suggest trying >= 5.8.0 - it has fixed internal-to-perl memory problems for me, this may be another area that has been beefed up between 5.6 and 5.8.

Re: Vanishing bug
by hv (Prior) on Jan 25, 2005 at 13:39 UTC

    I had a problem similar to this in my work application a while back, which I eventually tracked down to a bug in perl that was being triggered by the complexity of the operations inside the sort comparison subroutine.

    I don't remember the precise nature of the bug, but it was triggered in my case when the method invoked in the sort needed data not yet fetched from the database.

    I worked around it by adding an extra pass through the list to ensure the method was called once on each object (and the data from the database was therefore already cached) before the sort itself, for each of the affected sorts. My checkin message at the time said "add temporary fix to avoid sort subroutine scoping problems that affect all perls earlier than 5.8.0".

    Hugo