package main; use X; use SORT; my $p = new X(10); my$q = new X(20); my $r = new X(15); foreach my $obj (sort by_id ($p,$q,$r)){ print $obj->id; } package X; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(id); sub new { my $class =shift; my $self = { _id => shift }; bless $self, $class; } sub id { my $self = shift; return $self->{_id}; } 1; package SORT; use X; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(by_id); sub by_id { $a->id <=> $b->id; } 1;