source: trunk/npemap.org.uk/scripts/content/update_stats.pl @ 219

Last change on this file since 219 was 219, checked in by Dominic Hargreaves, 14 years ago

Add a perl module for some glue including templates, and modify update_stats.pl
to use it.

  • Property svn:executable set to *
File size: 1.2 KB
Line 
1#!/usr/bin/perl
2#
3# Copyright (c) 2006 Dominic Hargreaves
4# See accompanying file "LICENCE" for licence details
5
6use strict;
7use warnings;
8
9use lib qw(/home/dom/working/npemap.org.uk/perllib);
10
11use DBI;
12use NPEMap::Postcodes;
13use vars qw($dbname $dbhost $dbuser $dbpass);
14
15sub print_err;
16sub setup_dbh;
17
18# Read in database config
19my $config = 'npemap.conf';
20do $config or die "Can't read $config!\n";
21
22
23# No point in handling errors here since they'll get handled by the request
24# handler
25my $dbh;
26
27
28    # In case the database went away, make sure we have a connection
29    unless (setup_dbh()) {
30        print_internal_err('Error setting up database connection');
31    }
32
33build_home_stats($dbh);
34
35
36# No more requests to serve, so tidy up
37$dbh->disconnect;
38
39# Helper routines
40sub print_err {
41    my $err = shift;
42    print header("text/plain", "400 $err");
43    print "Error: $err\n";
44}
45
46sub print_internal_err {
47    my $err = shift;
48    print header("text/plain", "500 $err");
49    print "Error: $err\n";
50}
51
52sub setup_dbh {
53    # $dbh is global
54    my $data_source = "dbi:Pg:dbname=$dbname";
55    $data_source .= ";host=$dbhost" if $dbhost;
56    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
57}
Note: See TracBrowser for help on using the repository browser.