|
|
sybperl-l Archive
Up Prev Next
From: Monty Charlton <mcharlton at dtint dot com>
Subject: Re: callback question
Date: Apr 14 2003 8:22PM
Scott,
That worked perfectly, thank you very much.
For the archives, there was a minor typo in the subroutine name (test_cb
instead of sample_cb). Should be:
use Sybase::CTlib;
use FileHandle;
sub sample_cb {
my $fh = shift;
my $realfunc = sub {
my @args = @_;
print $fh "Put your own code here...\n";
};
return $realfunc;
}
my $fh = new FileHandle (">/path/to/file") or die ("$!");
my $test = sample_cb ($fh); ### Was test_cb ($fh)
ct_callback (CS_CLIENTMSG_CB, $test);
ct_callback (CS_SERVERMSG_CB, $test);
my $dbh = new Sybase::CTlib $user, $pass, $server, $app;
$dbh->ct_sql ("use foo");
Thanks again!
Monty
On Monday 14 April 2003 01:00 pm, Scott Zetlan wrote:
> I'd use a closure:
>
> use Sybase::CTlib;
> use FileHandle;
>
> sub sample_cb {
> my $fh = shift;
> my $realfunc = sub {
> my @args = @_;
> print $fh "Put your own code here...\n";
> };
> return $realfunc;
> }
>
> my $fh = new FileHandle (">/path/to/file") or die ("$!");
> my $test = test_cb ($fh);
>
> ct_callback (CS_CLIENTMSG_CB, $test);
> ct_callback (CS_SERVERMSG_CB, $test);
>
> my $dbh = new Sybase::CTlib $user, $pass, $server, $app;
> $dbh->ct_sql ("use foo");
>
> You can determine at run time (using a conf file, environment var, etc.)
> the path to the logfile, then create the callback function on the fly using
> the lexical closure (sample_cb creates an anonymous sub with reference
> stored in $realfunc).
>
> Scott
>
> Monty Charlton wrote:
>
> All,
>
> I am using ct_callback in my code for trapping errors. I call ct_callback
> like so:
>
> ct_callback(CS_CLIENTMSG_CB, \&msg_cb);
> ct_callback(CS_SERVERMSG_CB, \&srv_cb);
>
> Then my msg_cb srv_cb subroutines log to a file. The log file is currently
> hard-coded.
>
> I need to have the log file defined at the command line or in a config
> file. I suppose I need to pass another parameter to each of these
> functions. Short of defining a global variable to the log file path, is
> there a way to do this?
>
> Something like:
>
> sub msg_cb
> {
> my( $dbh, $number, $severity, $state, $line, $server, $proc, $msg,
> $userFileHandle ) = @_;
> print $userFileHandle "error...";
> return CS_SUCCEED;
> }
>
> Thanks.
>
> Monty
--
Monty Charlton
Digital Technology International
|