pglinx has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: php to perl
by kennethk (Abbot) on Mar 12, 2009 at 17:50 UTC
    What have you tried, after the help offered in Zip code distance search? This is, after all, not a code-writing service.
Re: php to perl
by ELISHEVA (Prior) on Mar 12, 2009 at 18:45 UTC

    Your fellow monks worked quite hard to help you out with this very same question over at this post: Zip code distance search. Please do not add a second post with a slightly different angle on the same question. We can't really give you the best possible help without seeing the whole history of answers and your response to them in one place.

    Over there you provided an attempt at Perl code and that was a good thing. If you are still confused, add a reply to your original post explaining (a) what was helpful so far and (b) what you still aren't sure about. Doing so will keep the original post on the Recently Active Threads list for another day. If you think posting the original PHP code will help, add it to your replay to your original post over there

    To further understand why this is a bad idea, please see this post by another user today who double posted a slightly different take on the same question.

    Best, beth

Re: php to perl
by jethro (Monsignor) on Mar 12, 2009 at 19:12 UTC

    Search CPAN for Math and you will find modules like Math::Trig that have all the functions like acos and deg2rad available in perl. The rest of the conversion seems trivial (like replacing "sub" for "function")

Re: php to perl
by CountZero (Bishop) on Mar 12, 2009 at 22:02 UTC
    PHP is like "Perl light" so it translates almost one-on-one (as an exercise try to spot the differences and understand why they are different):
    use strict; sub distance { my ( $lat1, $lon1, $lat2, $lon2, $unit ) = @_; my $theta = $lon1 - $lon2; my $dist = sin( deg2rad($lat1) ) * sin( deg2rad($lat2) ) + cos( deg2rad($lat1) ) * cos( deg2rad($lat2) ) * cos( deg2rad($theta) ); $dist = acos($dist); $dist = rad2deg($dist); my $miles = $dist * 60 * 1.1515; $unit = uc $unit; if ( $unit eq "K" ) { return ( $miles * 1.609344 ); } ## end if ( $unit eq "K" ) elsif ( $unit eq "N" ) { return ( $miles * 0.8684 ); } ## end elsif ( $unit eq "N" ) else { return $miles; } ## end else [ if ( $unit eq "K" ) } ## end sub distance sub acos { atan2( sqrt( 1 - $_[0] * $_[0] ), $_[0] ) } sub deg2rad { $_[0] * 0.0174532925 } sub rad2deg { $_[0] * 57.2957795 }
    print distance(60.1843160, -128.5069940, 29.46786, -98.53506, "M"), " +miles\n"; print distance(32.9697, -96.80322, 29.46786, -98.53506, "K"), " kilome +ters\n"; print distance(32.9697, -96.80322, 29.46786, -98.53506, "N"), " nautic +al miles\n";
    gives as output:
    2535.33852270174 miles 422.738930838725 kilometers 228.109395841006 nautical miles
    To be entirely correct you probably want to limit the number of decimals as so many decimals will not all be significant.

    And if you go all the way and make use of the existing modules at CPAN:

    use GIS::Distance; my $gis = GIS::Distance->new(); $gis->formula('Cosine'); print $gis->distance( 60.1843160, -128.5069940 => 29.46786, -98.53506 +) ->value('mile'), " miles\n"; print $gis->distance( 32.9697, -96.80322 => 29.46786, -98.53506 ) ->value('kilometer'), " kilometers\n"; print $gis->distance( 32.9697, -96.80322 => 29.46786, -98.53506 ) ->value('nautical_mile'), " nautical miles\n";

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      PHP is like "Perl light

      I'd say it's more like Perl--. :P

      And you didn't even know bears could type.

Re: php to perl
by eff_i_g (Curate) on Mar 12, 2009 at 18:30 UTC