database connect: protect with exceptions?

F

filippo

Hello,

hereafter is the command I use to connect my database;

my $dbhCampeggio = DBI->connect(
"DBI:pgPP:dbname=$DATABASE_NAME;host=$DATABASE_SERVER;port=5432",
"postgres",
"postgres",
{ RaiseError => 1}
) or die ( "Connessione al DATABASE non riuscita: $DBI::errstr");

$DATABASE_SERVER is passed from command line. I want my program to ask
interactively the user to write the name, If the user forget to pass
the database server at command line, and the program ask the user
whenever she doesn't write a valid database server.

How can I do?

Thanks
Filippo
 
T

Tad McClellan

filippo said:
$DATABASE_SERVER is passed from command line. I want my program to ask
interactively the user to write the name, If the user forget to pass
the database server at command line,


my $DATABASE_SERVER = shift @ARGV;
unless ( defined $DATABASE_SERVER ) {
print STDOUT 'enter the database server name: ';
$DATABASE_SERVER = <STDIN>;
chomp $DATABASE_SERVER;
}
 
F

Filippo

Tad McClellan ha scritto:
my $DATABASE_SERVER = shift @ARGV;
unless ( defined $DATABASE_SERVER ) {
print STDOUT 'enter the database server name: ';
$DATABASE_SERVER = <STDIN>;
chomp $DATABASE_SERVER;
}

thanks Tad but this isn't what I want: the unless clause accept any
value for DATABASE_SERVER. I want to try to connect and get the value
unless the connection is succeeded fine.

Thanks and best regards,

Filippo
 
J

J. Gleixner

Filippo said:
Tad McClellan ha scritto:


thanks Tad but this isn't what I want: the unless clause accept any
value for DATABASE_SERVER. I want to try to connect and get the value
unless the connection is succeeded fine.

Wrap it in another loop. Also see:

perldoc -f eval


use DBI;
my $dbh;
my $DATABASE_SERVER = shift @ARGV;

unless ( $dbh )
{

#... insert unless loop, from above, to set DATABASE_SERVER

eval {
$dbh = DBI->connect( ... $DATABASE_SERVER,...,
{ RaiseError => 1 } );
};
if ( $@ )
{
print "Couldn't connect using $DATABASE_SERVER.\n";
undef $DATABASE_SERVER;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top