Hmm, i can so you are only using do() to execute the SQL statements. So i'm assuming you are "blindly" executing stuff, without the need to read back data from the database. You might not need multiple programs or even subroutines to do this one.
Let's assume you have a list of SQL statements you want executed. You can just put them into text files, one per line, then do something like this:
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
use Carp;
use English;
my $dbh = DBI->connect('dburl', 'dbuser', 'dbpassword', {AutoCommit =>
+ 0, RaiseError => 0}) or croak($EVAL_ERROR);
foreach my $fname (qw[pdi pitchover contactlight engineshutdown]) {
print "Executing file $fname\n";
open($my $ifh, '<', $fname) or croak($ERRNO);
while((my $line = <$ifh>)) {
chomp $line; # Remove linebreak character
# Ignore empty lines and comments
next if($line eq '' || $line =~ /^\#/);
if(!$dbh->do($line)) {
# It's a 1202 program alarm, abort the landing
$dbh->rollback;
croak($dbh->errstr);
}
}
close $ifh;
}
# Tranquility base here, the Eagle has landed
$dbh->commit;
If you do more complex stuff that needs logic, you can split the program into perl modules. This would still use a single database handle, but allows you to do complicated interactive stuff. Let's start with eagly.pl, the main program:
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
use Carp;
use English;
# Load the .pm files from the current directory. Not really recommende
+d, but it's fine for testing and debugging
BEGIN {
unshift @INC, '.';
};
use PDI;
use Pitchover;
use ContactLight;
use EngineShutdown;
my $dbh = DBI->connect('dburl', 'dbuser', 'dbpassword', {AutoCommit =>
+ 0, RaiseError => 0}) or croak($EVAL_ERROR);
my @modules;
push @modules, PDI->new(dbh => $dbh);
push @modules, Pitchover->new(dbh => $dbh);
push @modules, ContactLight->new(dbh => $dbh);
push @modules, EngineShutdown->new(dbh => $dbh);
foreach my $module (@modules) {
if(!$module->executeProgram()) {
$dbh->rollback;
croak("Abort!");
}
}
# Tranquility base here, the Eagle has landed
$dbh->commit;
Ok, let's take a look at PDI.pm (each module would look similar, depending on what it does)
package PDI;
use strict;
use warnings;
use English;
sub new {
my ($proto, %config) = @_;
my $class = ref($proto) || $proto;
my $self = bless \%config, $class;
return $self;
}
sub executeProgram {
my ($self) = @_;
# Slew to PDI attitude
if(!$self->{dbh}->do("firststatement")) {
return 0;
}
# Calculate burn time
if(!$self->{dbh}->do("secondstatement")) {
return 0;
}
# do some more complex stuff, like reading data from the database
+and using that to
# decide if we need to open the helium valves
my $selsth = $self->{dbh}->prepare_cached("SELECT valvename, valve
+state FROM descentstage") or return 0;
if(!$selsth->execute) {
return 0;
}
while((my $line = $selsth->fetchrow_hashref)) {
# complex logic here
}
$selsth->finish;
return 1;
}
1;
Hope that helps a bit in designing your program.
perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'