Dear Perl Monks
I'm getting the message
Operation "<=>": no method found,
left argument in overloaded package Class::Measure::Length,
right argument in overloaded package Class::Measure::Length
from what I thought was just a simple sort
$hash{$b}<=>$hash{$a}
I found a work-around, by sprintf-ing the values in the hash - see code below - but I would like to (try to) understand what is going on.
I'm on Ubuntu 12.04
perl -v says
This is perl 5, version 14, subversion 2 (v5.14.2)
built for i686-linux-gnu-thread-multi-64int
The following script gives the
no method found message the second time round
use strict;
use Carp;
use GIS::Distance;
my $from_lat = 50.815717;
my $from_long = 4.436326;
my $to = {
Montana => [ 46.536026, -111.935062 ],
Correze => [ 45.341874, 1.79531 ],
ParisFR => [ 48.850407, 2.347304 ],
};
my %distance;
foreach my $demo ( 'solution', 'problem' ) {
print STDOUT "\n", "-" x 60, "\n\n";
print STDOUT "This is to demonstrate the $demo ...\n";
print STDOUT " Here comes the contents of the hash \%distance\n";
print STDOUT " (distances as calculated by GIS::Distance\n";
if ( $demo eq 'problem' ) {
print STDOUT " and stored as that value)\n";
}
elsif ( $demo eq 'solution' ) {
print STDOUT " and stored as sprintf \"%25.15f\", value )\n";
}
foreach my $place ( keys(%$to) ) {
my $gis = GIS::Distance->new();
my $to_lat = $to->{$place}->[0];
my $to_long = $to->{$place}->[1];
my $km = $gis->distance(
$from_lat, $from_long => $to_lat, $to_long, );
if ( $demo eq 'problem' ) {
$distance{$place} = $km;
}
elsif ( $demo eq 'solution' ) {
$distance{$place} = sprintf "%25.15f", $km;
}
print STDOUT " \$distance{$place} = $distance{$place}\n";
}
print STDOUT "\n";
print STDOUT
"And here comes the result of sorting the keys of that hash\n";
foreach
my $place(sort
{$distance{$b} <=> $distance{$a}}(
keys(%distance)))
{
printf STDOUT "%10s is %25.15f km from here\n",
$place, $distance{$place};
}
}