Subject: | Custom sort routine |
Hi,
I'm trying to use a custom sort routine with File::Next.
The example on the cpan page suggests something like this:
sort_files => sub { $a->[1] cmp $b->[1] }
However, that doesn't work - $a and $b are not defined.
Looking at the code in Next.pm, you use this for sort_standard:
return $_[0]->[1] cmp $_[1]->[1]
Now, I want to sort by file size, so I've written a routine like this:
sub sort_by_size($$) {
my $sizea = -f $_[0]->[2] ? -s $_[0]->[2] : 0;
my $sizeb = -f $_[1]->[2] ? -s $_[1]->[2] : 0;
$sizea <=> $sizeb;
}
and I use it like this:
my $files = File::Next::files( { sort_files => \&sort_by_size, }, $srcdir );
Does that look OK?
One question: why do you use subroutine prototypes? Why not just pass
the array to the sort routine explicitly by reference ?
Thanks,
R.