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

Hi monks, I am using Sybase::CTlib module in PERL. The following message is being displayed. Server message: Message number: 208, Severity 16, State 1, Line 1 Server 'XXXXXXXXX' Message String: Table Name not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output). The above message is being displayed from the Sybase server end. But, what I need is to capture this message as a PERL string variable.
  • Comment on How can I catch a Server message displayed in Sybase::CTlib module

Replies are listed 'Best First'.
Re: How can I catch a Server message displayed in Sybase::CTlib module
by Anonymous Monk on May 06, 2009 at 12:07 UTC

      Actually the Examples section of Sybase::CTlib clearly shows how to set a callback trap for Server side errors.

      -derby
        Derby, Thank you for the reply. I am very new to this DB modules of PERL. I have seen the examples link earlier. What I found out was, I can print the message code and message statements on standard error or console. But, what I need is to capture the entire message in a single string. I came to know that "CS_HAFAILOVER" variable will hold the Error code. Similarly, do we have any vatiable which holds the entire error message, because I need to print the error message in a Log file
Re: How can I catch a Server message displayed in Sybase::CTlib module
by derby (Abbot) on May 06, 2009 at 12:20 UTC
Re: How can I catch a Server message displayed in Sybase::CTlib module
by shmem (Chancellor) on May 06, 2009 at 14:39 UTC

    You could redirect STDERR for the time you make calls into that module.

    package Foo; sub foo { warn "blorf!" } 1;
    #!/usr/bin/perl use Foo; use strict; use warnings; sub call (&); call { Foo::foo(); }; if ($@) { warn "foo reported: '$@'\n"; } ## subs # sub call (&) { my $coderef = shift; my $message; open my $fh , '>&', \*STDERR; close STDERR; open STDERR, '>', \$message; $coderef->(); open STDERR, ">&", $fh; $@ = $message; } __END__ foo reported: 'blorf! at Foo.pm line 3. '

    That way all you have to do is wrap the places where you call into Sybase::CTlib into a block and dispatch that to the call() function.

      Hi shmem,

      Thank you very much.
      I have executed the above code, and I have got the expected result.

      I still need some help. Sorry if it is silly.

      Still, my question is,
      where can I send the below err statements into log file. I have tried them and was not able to throw into log file.

      ct_callback(CS_CLIENTMSG_CB, \&msg_cb);
      ct_callback(CS_SERVERMSG_CB, "srv_cb");

      sub msg_cb {
      my($layer, $origin, $severity, $number, $msg, $osmsg, $dbh) = @_;

      printf STDERR "\nOpen Client Message: (In msg_cb)\n";
      printf STDERR "Message number: LAYER = (%ld) ORIGIN = (%ld) ",
      $layer, $origin;
      printf STDERR "SEVERITY = (%ld) NUMBER = (%ld)\n",
      $severity, $number;
      printf STDERR "Message String: %s\n", $msg;
      if (defined($osmsg)) {
      printf STDERR "Operating System Error: %s\n", $osmsg;
      }
      CS_SUCCEED;
      }

      sub srv_cb {
      my($dbh, $number, $severity, $state, $line, $server,
      $proc, $msg) = @_;

      # If $dbh is defined, then you can set or check attributes
      # in the callback, which can be tested in the main body
      # of the code.

      printf STDERR "\nServer message: (In srv_cb)\n";
      printf STDERR "Message number: %ld, Severity %ld, ",
      $number, $severity;

      printf STDERR "State %ld, Line %ld\n", $state, $line;

      if (defined($server)) {
      printf STDERR "Server '%s'\n", $server;
      }

      if (defined($proc)) {
      printf STDERR " Procedure '%s'\n", $proc;
      }

      printf STDERR "Message String: %s\n", $msg; CS_SUCCEED;
      }

      please, help required...

      Thanks in advance!!!

        open file, print to filehandle you opened, close filehandle