I've been using the Related Entries MT plugin for quite sometime now with great effect to show related entries on each entries' permalink page. I just recently reenabled it in this blog and noticed that it was giving incorrect results (i.e. it was showing the last n entries from the site overall, rather than last n related entries). After pouring through the comments on the related entries site and some of my own investigations, I found the solution. The problem stems from an issue with the plugin. Out of the box, copious warnings are issued about unitialized variables and array offsets but the plugin works fine. In the comments, a patch is suggested. Applying it removes the warnings but causes the problem that I was experiencing. (I applied the patch without thinking and didn't bother to check the results). I decided to try the simplier suggestion found in the code to explictly give the $method variable a value:
my $method = "primary_category";
and then removed the my keyword from the next line. This removed the warning about the uninitialized variable but left one about the array indexes. The problem line in question was:
splice(@entries, $lastn)
I'm no means a perl person (I favor python), but some quick web searches helped me decipher the intent of this line. The splice function in this case takes an array and an offset and truncates the array starting at that offset. I surmised that this warning is being raised when the size of the array is smaller than the offset being used (i.e. Show me 10 related entries in a category that only has 5 entries). A few more web searches on how to find the size of an array in perl yielded this code to fix the problem:
if ( scalar(@entries) > $lastn) {
splice(@entries, $lastn)
}
Voila! That feels pretty good to a person who actively avoids perl at all costs. I realize that perl undoubtable has a terser syntax for what I just wrote, but to me that feels the cleanest and most straightforward approach to the problem.