tcheungcm has asked for the wisdom of the Perl Monks concerning the following question:

I used Sybase DBlib to connect to a sybase erver and then use dbuse to change database. However, it displayed STDERR on the command prompt. Instead, I would like to capture the error and suppress them. Could you advise how to do?

sub generate_syboutput{ my $curr_dbserver=$_[0]; my $curr_db=$_[1]; $curr_db=~ s/\s//g; my $dbuser=$_[2]; my $dbpasswd=$_[3]; my $sp_name=$_[4]; my $fh=$_[5]; my $sqlcmd; my $dbh1=Sybase::DBlib->new($dbuser, $dbpasswd, $curr_dbserver); if (!$dbh1) { print "Connection Failed\n"; exit(0); } print "Debug Before dbuse\n"; if (!$dbh1->dbuse($curr_db)) { print "Debug this db cannot use\n"; } }

Output

Debug Before dbuse

Msg 10351, Level 14, State 1

Server 'MUTRDP3', Line 1

Server user id 2038 is not a valid user in database 'TestDB1'

DB-Library error: General SQL Server error: Check messages from the SQL Server.

Debug this db cannot use

I noticed there is dberrhandle or dbmsghandle that can be used for error handle but i don't know how to use it. please advice. do you have any example?

Replies are listed 'Best First'.
Re: Handle DBlib error
by blindluke (Hermit) on Dec 10, 2014 at 07:37 UTC

    It seems that the question you have has already been asked before. Look at this thread: Error Handeling in Sybase::Dblib. Especially this excellent reply by mpeppler, where he explains how to create a message handler subroutinne and then install it using:

    dbmsghandle(\&message_handler);

    Which should be exactly what you are looking for. Good luck!

    - Luke