in reply to Variable will not stay shared in subroutine

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.