M
Mothra
Hello everyone,
I have a perl script (see below) that uses dispatch tables to set values,
however
subs get_lat_N and get_lat_S are almost identical :-( this tells me thatt I
am
doing something wrong. How can I modify my dispatch table structure to avoid
duplicate code?
Thanks
Mothra
-------script below--------
use HTTP::Request::Common;
use POSIX qw(floor);
#use Data:umper;
use HTML::Form;
use LWP::UserAgent;
#use LWP:ebug qw(+);
use strict;
use warnings;
my %dispatch_table_lat = (
'N' => \&get_lat_N,
'S' => \&get_lat_S
);
my %dispatch_table_lon = (
'E' => \&get_long_E,
'W' => \&get_long_W
);
our %location = ();
#open (OUT, ">> d:/tmp/fetched_cities.txt") or die "Can't open file:$!\n";
while (<DATA>) {
/(\w+),\s+(\w+)\s+(\d+)\s+(\d+)\s+(\w)\s+(\d+)\s+(\d+)\s+(\w)/;
$dispatch_table_lat{$5}->( $3, $4 );
$dispatch_table_lon{$8}->( $6, $7 );
$location{'zz1'} = floor( $location{'xx1'} / 15 );
my ( $sunrise, $sunset ) = get_sunrise();
print "$1 $2 sunrise: $sunrise sunset: $sunset\n";
#print OUT "$1, $2 $3 $4 $5 $6 $7 $8 sunrise: $sunrise sunset:
$sunset\n";
}
close OUT;
sub get_lat_N {
my ( $degree, $minute ) = @_;
$location{'yy0'} = '1';
$location{'yy1'} = $degree;
$location{'yy2'} = $minute;
return;
}
sub get_lat_S {
my ( $degree, $minute ) = @_;
$location{'yy0'} = '-1';
$location{'yy1'} = $degree;
$location{'yy2'} = $minute;
return;
}
sub get_long_E {
my ( $degree, $minute ) = @_;
$location{'zz0'} = '1';
$location{'xx0'} = '1';
$location{'xx1'} = $degree;
$location{'xx2'} = $minute;
return;
}
sub get_long_W {
my ( $degree, $minute ) = @_;
$location{'zz0'} = '-1';
$location{'xx0'} = '-1';
$location{'xx1'} = $degree;
$location{'xx2'} = $minute;
return;
}
sub get_sunrise {
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.73');
my $req =
HTTP::Request->new(
GET => 'http://aa.usno.navy.mil/data/docs/RS_OneDay.html' );
my $res = $ua->request($req);
my @form = HTML::Form->parse( $res->content, $res->base() );
#xxy is year
#xxm is month
#xxd is day
$form[1]->value( 'xxy', "2007" );
$form[1]->value( 'xxm', "2" );
$form[1]->value( 'xxd', "20" );
#xx0 is -1 or 1 long 1 for east -1 for west
#xx1 is long in degrees
#xx2 is long in minites
$form[1]->value( 'xx0', $location{'xx0'} );
$form[1]->value( 'xx1', $location{xx1} );
$form[1]->value( 'xx2', $location{'xx2'} );
#yy0 is -1 for west 1 for east latitude
#yy1 is degrees
#yy2 is minites
$form[1]->value( 'yy0', $location{'yy0'} );
$form[1]->value( 'yy1', $location{'yy1'} );
$form[1]->value( 'yy2', $location{'yy2'} );
#zz1 is offset from GMT
#zz0 is -1 west of GMT 1 east
$form[1]->value( 'zz1', $location{'zz1'} );
$form[1]->value( 'zz0', $location{'zz0'} );
$req = $form[1]->make_request;
$req = $ua->request($req);
$req->content =~ /\s+Sunrise\s+(\d+:\d+)\s+/;
#print $req->content;
my $sunrise = $1;
$req->content =~ /\s+Sunset\s+(\d+:\d+)\s+/;
my $sunset = $1;
return ( $sunrise, $sunset );
}
__DATA__
Aberdeen, Scotland 57 9 N 2 9 W
Adelaide, Australia 34 55 S 138 36 E
Algiers, Algeria 36 50 N 3 0 E
Amsterdam, Netherlands 52 22 N 4 53 E
Ankara, Turkey 39 55 N 32 55 E
Asuncion, Paraguay 25 15 S 57 40 W
I have a perl script (see below) that uses dispatch tables to set values,
however
subs get_lat_N and get_lat_S are almost identical :-( this tells me thatt I
am
doing something wrong. How can I modify my dispatch table structure to avoid
duplicate code?
Thanks
Mothra
-------script below--------
use HTTP::Request::Common;
use POSIX qw(floor);
#use Data:umper;
use HTML::Form;
use LWP::UserAgent;
#use LWP:ebug qw(+);
use strict;
use warnings;
my %dispatch_table_lat = (
'N' => \&get_lat_N,
'S' => \&get_lat_S
);
my %dispatch_table_lon = (
'E' => \&get_long_E,
'W' => \&get_long_W
);
our %location = ();
#open (OUT, ">> d:/tmp/fetched_cities.txt") or die "Can't open file:$!\n";
while (<DATA>) {
/(\w+),\s+(\w+)\s+(\d+)\s+(\d+)\s+(\w)\s+(\d+)\s+(\d+)\s+(\w)/;
$dispatch_table_lat{$5}->( $3, $4 );
$dispatch_table_lon{$8}->( $6, $7 );
$location{'zz1'} = floor( $location{'xx1'} / 15 );
my ( $sunrise, $sunset ) = get_sunrise();
print "$1 $2 sunrise: $sunrise sunset: $sunset\n";
#print OUT "$1, $2 $3 $4 $5 $6 $7 $8 sunrise: $sunrise sunset:
$sunset\n";
}
close OUT;
sub get_lat_N {
my ( $degree, $minute ) = @_;
$location{'yy0'} = '1';
$location{'yy1'} = $degree;
$location{'yy2'} = $minute;
return;
}
sub get_lat_S {
my ( $degree, $minute ) = @_;
$location{'yy0'} = '-1';
$location{'yy1'} = $degree;
$location{'yy2'} = $minute;
return;
}
sub get_long_E {
my ( $degree, $minute ) = @_;
$location{'zz0'} = '1';
$location{'xx0'} = '1';
$location{'xx1'} = $degree;
$location{'xx2'} = $minute;
return;
}
sub get_long_W {
my ( $degree, $minute ) = @_;
$location{'zz0'} = '-1';
$location{'xx0'} = '-1';
$location{'xx1'} = $degree;
$location{'xx2'} = $minute;
return;
}
sub get_sunrise {
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.73');
my $req =
HTTP::Request->new(
GET => 'http://aa.usno.navy.mil/data/docs/RS_OneDay.html' );
my $res = $ua->request($req);
my @form = HTML::Form->parse( $res->content, $res->base() );
#xxy is year
#xxm is month
#xxd is day
$form[1]->value( 'xxy', "2007" );
$form[1]->value( 'xxm', "2" );
$form[1]->value( 'xxd', "20" );
#xx0 is -1 or 1 long 1 for east -1 for west
#xx1 is long in degrees
#xx2 is long in minites
$form[1]->value( 'xx0', $location{'xx0'} );
$form[1]->value( 'xx1', $location{xx1} );
$form[1]->value( 'xx2', $location{'xx2'} );
#yy0 is -1 for west 1 for east latitude
#yy1 is degrees
#yy2 is minites
$form[1]->value( 'yy0', $location{'yy0'} );
$form[1]->value( 'yy1', $location{'yy1'} );
$form[1]->value( 'yy2', $location{'yy2'} );
#zz1 is offset from GMT
#zz0 is -1 west of GMT 1 east
$form[1]->value( 'zz1', $location{'zz1'} );
$form[1]->value( 'zz0', $location{'zz0'} );
$req = $form[1]->make_request;
$req = $ua->request($req);
$req->content =~ /\s+Sunrise\s+(\d+:\d+)\s+/;
#print $req->content;
my $sunrise = $1;
$req->content =~ /\s+Sunset\s+(\d+:\d+)\s+/;
my $sunset = $1;
return ( $sunrise, $sunset );
}
__DATA__
Aberdeen, Scotland 57 9 N 2 9 W
Adelaide, Australia 34 55 S 138 36 E
Algiers, Algeria 36 50 N 3 0 E
Amsterdam, Netherlands 52 22 N 4 53 E
Ankara, Turkey 39 55 N 32 55 E
Asuncion, Paraguay 25 15 S 57 40 W