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";'

In reply to Re: designing a program - your wisdom needed by cavac
in thread designing a program - your wisdom needed by SpaceCowboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.