in reply to OO and sorting

$a and $b are special globals. When you defined by_id() in package SORT, you've actually defined { $SORT::a->id <=> $SORT::b->id }. What you'll need to do is pass the package name in (somehow) and put it into your function definition.