It has been suggested that I'm not using MVC separation, which is fair, as I don't really see MVC separation. I found the POD for
Dancer::Plugin::Database
, and have modified my package to use it. weather
is the old one, weather2
is the new one using the plugin.package TestApp ; use Dancer ':syntax' ; use Dancer::Plugin::Database ; use lib '/home/jacoby/lib' ; use MyDB 'db_connect' ; use DateTime ; use Data::Dumper ; our $VERSION = '0.1' ; get '/' => sub { template 'index' ; } ; get '/weather' => sub { my $dbh = db_connect() ; my $sql = <<SQL; # Last Eight Hours SELECT time , AVG( temp_f ) FROM weather WHERE zip = "47909" AND HOUR( TIMEDIFF( SYSDATE() , time ) ) < 9 GROUP BY HOUR(time) ORDER BY time SQL my $hr = $dbh->selectall_arrayref( $sql ) or croak $dbh->errstr ; my @hr ; for my $point ( @$hr ) { my ( $time, $temp ) = @$point ; my @time = split /\D+/ , $time ; my $dt = DateTime->new( year => $time[0] , month => $time[1] , day => $time[2] , hour => $time[3] , minute => $time[4] , second => $time[5] , ) ; my $data ; $data->{ time } = $dt->format_cldr( 'h:mm a' ) ; $data->{ temp } = sprintf '%.1f', $temp ; next if $data->{ time } !~ /00/ ; push @hr, $data ; } my $string = 'Temperature in West Lafayette for the last 8 hours' ; my $hr_ptr = \@hr ; template 'weather' , { string => $string , weather => $hr_ptr } ; } ; get '/weather2' => sub { my $string = 'Temperature in West Lafayette for the last 8 hours' ; my $sth = database->prepare( 'SELECT time , AVG( temp_f ) temp FROM weather WHERE zip = "47909" AND HOUR( TIMEDIFF( SYSDATE() , time ) ) < 9 GROUP BY HOUR(time) ORDER BY time ' ) ; $sth->execute( ) ; template 'weather' , { string => $string , xxx => $sth->fetchall_arrayref( ) , } ; } ; true ;
I have been pointed to http://search.cpan.org/~xsawyerx/Dancer-1.3091/lib/Dancer/Tutorial.pod, and, beyond my wanting to be more specific in the munging of the data,
weather
is very similar to "Our First Route Handler". (How I store some data is not necessarily how I want to display some data.)weather2 uses http://search.cpan.org/~bigpresh/Dancer-Plugin-Database-1.60/lib/Dancer/Plugin/Database.pm and you can see that there's much less code in there. The stuff I put into MyDB exists in Dancer::Plugin::Database, and I probably could mess with the data before sending it on, instead of hooking up
$sth->fetchall_arrayref()
to the output.The big thing I see here, which I could do something about with weather but probably not with the plugin-using weather2, is the SQL code being here. I would much rather shove that SQL off into a library somewhere, which I've done elsewhere, than have it at this point. But maybe, that's an aspect of me not really getting the MVC thing yet.