1

I am completely new to Perl . I have a Perl script which check connection to a oracle database running in a container . Here is my code

#!/usr/bin/perl
use DBI;

$\="\n";

print "Connecting to DB..";


my $dbh = DBI->connect('dbi:Oracle:host=oracle;sid=ORCLCDB;port=1521', 'books_admin/MyPassword', '',{ RaiseError => 1, AutoCommit => 0 })or die print ("could not connect! $DBI::errstr \n");

Using this script I'm able to connect with oracle database . But this script doesn't give any status output to my terminal . How can i check the script is connected to database?. I know bash has $? for checking the status of previously executed cmd. Do we have something similar in Perl?

Here is what i want Output "Successfully connected to Oracle-db" when the connection is OK and failed status when script can't connect to database

Note:This code give error status to my terminal. My Perl version is v5.16.3

MT0
  • 143,790
  • 11
  • 59
  • 117
Pratheesh
  • 565
  • 4
  • 19
  • If you put the command in an `eval` block, you can capture the output with `$@` -- see https://perldoc.perl.org/functions/eval.html – richyen Oct 31 '19 at 06:37
  • Peruse [perlvar](https://perldoc.perl.org/perlvar.html) for a variety of goodies (`$?` among them). Otherwise, it depends on the library / function (etc) and how it behaves; so read docs to see what they do on failure and then work with that. Also, complex libraries (like `DBI`) often have specific methods to query status. – zdim Oct 31 '19 at 08:31
  • @richyen The `eval` block and `$@` are useful if code in the block throws an exception (`die`s), since we get the control back (the program isn't terminated); the exception gets "caught." Without an exception raised there should be nothing in `$@` so it can't be used for general queries of status of previous commands (see it under [Error Variables in perlvar](https://perldoc.perl.org/perlvar.html#Error-Variables)). And many (most) libraries _don't_ throw (`die`) on error but (may) set `$?` (and perhaps other related globals, see `perlvar`) and may return codes. – zdim Oct 31 '19 at 08:49

1 Answers1

3

The documentation for the connect() method says this:

Establishes a database connection, or session, to the requested $data_source. Returns a database handle object if the connection succeeds. Use $dbh->disconnect to terminate the connection.

If the connect fails (see below), it returns undef and sets both $DBI::err and $DBI::errstr. (It does not explicitly set $!.) You should generally test the return status of connect and print $DBI::errstr if it has failed.

So your code could be as obvious as:

my $dbh = DBI->connect(...);
if ($dbh) {
  say 'Connection ok';
} else {
  die "Connection error: $DBI::errstr";
}

However, you're using RaiseError => 1, so your program will die rather than returning undef. So if your program gets past that line, you know the connection was successful.

my $dbh = DBI->connect(...);
say 'Connection ok';
Community
  • 1
  • 1
Dave Cross
  • 68,119
  • 3
  • 51
  • 97