in reply to Perl script to check database works or does it?

Write subs not comments, write

my @dbs = ( [ 'OWNER', 'dbi:...', 'user', 'pass' ], [ 'OWNER', 'dbi:...', 'user', 'pass' ], ); for my $db( @dbs ){ LogStats( @$db ); } ... sub LogStats { my( $filename, $dsn, $user, $pass ) = @_; ... my $dbh = DBI->connect( $dsn, $user, $pass, {qw/RaiseError 1/} ); open my($fh), ... ## QueryOne( $dbh, $fh, ... ); CreateTableStats( $dbh, $fh, ... ); PopulateTableStats( $dbh, $fh, ... ); DumpTableStats( $dbh, $fh, ... ); }

Note the RaiseError, saves you from writing "or die" all over

Then if there is a problem in Query4 on line 151 you can debug it independently from rest of the program

Closing the statement handle inside this loop is your mistake

If you write this way you don't need to explicitly close it

sub Query4Over500 { my ( $dbh, $FHExtract, $tb_name ) = @_; $tb_name = $dbh->quote_identifier( $tb_name ); my $sql = "select * from $tb_name"; my $sth = $dbh->prepare( $sql ); $sth->execute; while (my $aref = $sth->fetchrow_arrayref) { print $FHExtract ToCsv( $aref ); } }

Replies are listed 'Best First'.
Re^2: Perl script to check database works or does it?
by chris01010 (Novice) on Jan 05, 2016 at 10:36 UTC

    Agreed Subs are best and I'll probably rework the script. To be honest I was being slightly "lazy" in the fact I never intended the script to be so large. It just grew as I wrote it and additional requirements were added.

    Thanks for the pointers