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}; } }

In reply to Why am I getting message «Operation "<=>": no method found,» ? by RCH

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.