#!/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; #### #!/usr/bin/env perl use strict; use warnings; use DBI; use Carp; use English; # Load the .pm files from the current directory. Not really recommended, 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; #### 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, valvestate FROM descentstage") or return 0; if(!$selsth->execute) { return 0; } while((my $line = $selsth->fetchrow_hashref)) { # complex logic here } $selsth->finish; return 1; } 1;