in reply to Perl : DBI and Oracle
Mind some constructive style suggestions?
First, I'm leery of declaring all of your variables at the start. They're effectively global -- at least, shared among all subroutines. That gives you some of the same drawbacks as global variables, most notably that you can whack one somewhere and not notice it until you reach another part of the code.
Second, calling subroutines with the leading ampersand is almost always wrong. It's right in this case, but only because you're using prototypes. Since you do want to pass arguments to the subs, drop the prototypes, and you can get rid of the ampersand. I suggest avoiding prototypes unless you want to act like a built-in operator. In other words, most of the time, you don't need them.
Besides that, it's generally a lot clearer to use named arguments instead of accessing elements of @_ directly. The exception is when you want to make it very clear that you're modifying an argument in place.
Here's how I would have written things:
#!/usr/bin/perl -w use DBI; use strict; $ENV{"ORACLE_HOME"}="ORACLE_HOME=/oravl01/oracle/8.0.6/"; $ENV{"TWO_TASK"}="nxt1"; my $dbh = sqlConnect("alan1", "alan1"); unless ($dbh) { showSQLError( $DBI::errstr ); exit; } getRef( $dbh ); sqlDisconnect( $dbh ); sub sqlConnect { my ( $username, $password ) = @_; my $connectString = join '/', $username, $password; return DBI->connect('dbi:Oracle:',$connectString); } sub sqlDisconnect { my $dbh = shift; $dbh->disconnect; } sub getRef { my $dbh = shift; my $statement = "select OI_REFERENCE from OPEN_ISSUES order by OI_ +REFERENCE asc"; print "$statement\n"; my $sth = $dbh->prepare($statement) or showSQLError($dbh->errstr); $sth->execute or showSQLError($sth->errstr); print "\n#"; while(my ($oir) = $sth->fetchrow_array) { print "\n- $oir"; } print "\n#\n"; } sub showSQLError { print "<SCRIPT> alert('$_[0]');</SCRIPT>"; }
If you set the RaiseError attribute on the database handle, you can do away with showSQLError too.
|
|---|