Juerd hit the problem. The underlying issue is a naming issue, both subroutines have globally visible names, but if you call them both multiple times, there are many different lexical variables those names could be bound to. Perl guesses that the inner remains bound to the first lexical it ever saw that associated with, and that is likely wrong. (This is a complex issue, but there is no way past it.)
Here is a quick rewrite:
sub sort_results {
#========================================
# Sort results in descending order
# by date and ascending by document name
# within a date.
#========================================
my $self = shift;
my $database = $self->{_database};
my $sort_sub = sub {
$database->[$b]->{numeric_date} <=> $database->[$a]->{numeric_date
+}
or
$database->[$a]->{document_name} cmp $database->[$b]->{document_na
+me}
or
$a <=> $b;
};
@{$self->{_search_hits}} = sort $sort_sub @{$self->{_search_hits}};
+
}
Note that the use of an anonymous function means that the function is lexically named, and is always bound to the lexical variable in its name space. The previous naming issue is now just gone.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.